Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpRzyxVector.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Rzyx angle parameterization for the rotation.
32  * Rzyx(phi,theta,psi) = Rot(z,phi)Rot(y,theta)Rot(x,psi)
33  *
34 *****************************************************************************/
35 
43 #include <math.h>
44 #include <visp3/core/vpRzyxVector.h>
45 
49 
52 
59 vpRzyxVector::vpRzyxVector(double phi, double theta, double psi) : vpRotationVector(3) { build(phi, theta, psi); }
60 
67 
75 
78 
80 vpRzyxVector::vpRzyxVector(const std::vector<double> &rzyx) : vpRotationVector(3) { build(rzyx); }
81 
82 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
95 {
96  build(R);
97  return *this;
98 }
99 
108 {
109  build(tu);
110  return *this;
111 }
112 
120 void vpRzyxVector::buildFrom(double phi, double theta, double psi)
121 {
122  build(phi, theta, psi);
123 }
124 
130 {
131  build(rzyx);
132  return *this;
133 }
134 
139 vpRzyxVector vpRzyxVector::buildFrom(const std::vector<double> &rzyx)
140 {
141  build(rzyx);
142  return *this;
143 }
144 #endif
145 
157 {
158  const unsigned int index_0 = 0;
159  const unsigned int index_1 = 1;
160  const unsigned int index_2 = 2;
161  double nx = R[index_0][index_0];
162  double ny = R[index_1][index_0];
163 
164  double COEF_MIN_ROT = 1e-6;
165  double phi;
166 
167  if ((fabs(nx) < COEF_MIN_ROT) && (fabs(ny) < COEF_MIN_ROT)) {
168  phi = 0;
169  }
170  else {
171  phi = atan2(ny, nx);
172  }
173  double si = sin(phi);
174  double co = cos(phi);
175 
176  double nz = R[index_2][index_0];
177  double theta = atan2(-nz, (co * nx) + (si * ny));
178 
179  double ax = R[index_0][index_2];
180  double ay = R[index_1][index_2];
181  double ox = R[index_0][index_1];
182  double oy = R[index_1][index_1];
183 
184  double psi = atan2((si * ax) - (co * ay), (-si * ox) + (co * oy));
185 
186  build(phi, theta, psi);
187 
188  return *this;
189 }
190 
198 {
200  R.build(tu);
201  build(R);
202 
203  return *this;
204 }
205 
212 vpRzyxVector &vpRzyxVector::build(const double &phi, const double &theta, const double &psi)
213 {
214  const unsigned int index_0 = 0;
215  const unsigned int index_1 = 1;
216  const unsigned int index_2 = 2;
217  data[index_0] = phi;
218  data[index_1] = theta;
219  data[index_2] = psi;
220  return *this;
221 }
222 
227 {
228  if (rzyx.size() != 3) {
229  throw(vpException(vpException::dimensionError, "Cannot construct a R-zyx vector from a %d-dimension col vector",
230  rzyx.size()));
231  }
232  const unsigned int val_3 = 3;
233  for (unsigned int i = 0; i < val_3; ++i) {
234  data[i] = rzyx[i];
235  }
236 
237  return *this;
238 }
239 
243 vpRzyxVector &vpRzyxVector::build(const std::vector<double> &rzyx)
244 {
245  if (rzyx.size() != 3) {
246  throw(vpException(vpException::dimensionError, "Cannot construct a R-zyx vector from a %d-dimension std::vector",
247  rzyx.size()));
248  }
249  const unsigned int val_3 = 3;
250  for (unsigned int i = 0; i < val_3; ++i) {
251  data[i] = rzyx[i];
252  }
253 
254  return *this;
255 }
256 
281 {
282  for (unsigned int i = 0; i < dsize; ++i) {
283  data[i] = v;
284  }
285 
286  return *this;
287 }
288 
316 {
317  if (rzyx.size() != 3) {
318  throw(vpException(vpException::dimensionError, "Cannot set a R-zyx vector from a %d-dimension col vector",
319  rzyx.size()));
320  }
321  const unsigned int val_3 = 3;
322  for (unsigned int i = 0; i < val_3; ++i) {
323  data[i] = rzyx[i];
324  }
325 
326  return *this;
327 }
328 
329 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
351 vpRzyxVector &vpRzyxVector::operator=(const std::initializer_list<double> &list)
352 {
353  if (list.size() > size()) {
354  throw(vpException(
356  "Cannot set Euler x-y-z vector out of bounds. It has only %d values while you try to initialize with %d values",
357  size(), list.size()));
358  }
359  std::copy(list.begin(), list.end(), data);
360  return *this;
361 }
362 #endif
363 END_VISP_NAMESPACE
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:148
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:1104
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
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:184
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.