Visual Servoing Platform  version 3.6.1 under development (2025-02-01)
vpRxyzVector.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  * Rxyz angle parameterization for the rotation.
32  * Rxyz(phi,theta,psi) = Rot(x,phi)Rot(y,theta)Rot(z,psi).
33  */
34 
41 #include <math.h>
42 
43 #include <visp3/core/vpRxyzVector.h>
44 
45 BEGIN_VISP_NAMESPACE
46 const unsigned int vpRxyzVector::constr_val_3 = 3;
49 
52 
59 vpRxyzVector::vpRxyzVector(double phi, double theta, double psi) : vpRotationVector(constr_val_3) { buildFrom(phi, theta, psi); }
60 
67 
75 
77 vpRxyzVector::vpRxyzVector(const vpColVector &rxyz) : vpRotationVector(constr_val_3) { buildFrom(rxyz); }
78 
80 vpRxyzVector::vpRxyzVector(const std::vector<double> &rxyz) : vpRotationVector(constr_val_3) { buildFrom(rxyz); }
81 
90 {
91  double COEF_MIN_ROT = 1e-6;
92  double phi;
93  const unsigned int index_0 = 0;
94  const unsigned int index_1 = 1;
95  const unsigned int index_2 = 2;
96 
97  if ((fabs(R[index_1][index_2]) < COEF_MIN_ROT) && (fabs(R[index_2][index_2]) < COEF_MIN_ROT)) {
98  phi = 0;
99  }
100  else {
101  phi = atan2(-R[index_1][index_2], R[index_2][index_2]);
102  }
103 
104  double si = sin(phi);
105  double co = cos(phi);
106  double theta = atan2(R[index_0][index_2], (-si * R[index_1][index_2]) + (co * R[index_2][index_2]));
107  double psi = atan2((co * R[index_1][index_0]) + (si * R[index_2][index_0]), (co * R[index_1][index_1]) + (si * R[index_2][index_1]));
108 
109  buildFrom(phi, theta, psi);
110 
111  return *this;
112 }
113 
121 {
123  R.buildFrom(tu);
124  buildFrom(R);
125 
126  return *this;
127 }
128 
135 vpRxyzVector &vpRxyzVector::buildFrom(const double &phi, const double &theta, const double &psi)
136 {
137  const unsigned int index_0 = 0;
138  const unsigned int index_1 = 1;
139  const unsigned int index_2 = 2;
140  data[index_0] = phi;
141  data[index_1] = theta;
142  data[index_2] = psi;
143  return *this;
144 }
145 
150 {
151  const unsigned int val_3 = 3;
152  if (rxyz.size() != val_3) {
153  throw(vpException(vpException::dimensionError, "Cannot construct a R-xyz vector from a %d-dimension col vector",
154  rxyz.size()));
155  }
156  for (unsigned int i = 0; i < val_3; ++i) {
157  data[i] = rxyz[i];
158  }
159 
160  return *this;
161 }
162 
166 vpRxyzVector &vpRxyzVector::buildFrom(const std::vector<double> &rxyz)
167 {
168  const unsigned int val_3 = 3;
169  if (rxyz.size() != val_3) {
170  throw(vpException(vpException::dimensionError, "Cannot construct a R-xyz vector from a %d-dimension std::vector",
171  rxyz.size()));
172  }
173  for (unsigned int i = 0; i < val_3; ++i) {
174  data[i] = rxyz[i];
175  }
176 
177  return *this;
178 }
179 
204 {
205  for (unsigned int i = 0; i < dsize; ++i) {
206  data[i] = v;
207  }
208 
209  return *this;
210 }
211 
239 {
240  const unsigned int val_3 = 3;
241  if (rxyz.size() != val_3) {
242  throw(vpException(vpException::dimensionError, "Cannot set a R-xyz vector from a %d-dimension col vector",
243  rxyz.size()));
244  }
245  for (unsigned int i = 0; i < val_3; ++i) {
246  data[i] = rxyz[i];
247  }
248 
249  return *this;
250 }
251 
252 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
274 vpRxyzVector &vpRxyzVector::operator=(const std::initializer_list<double> &list)
275 {
276  if (list.size() > size()) {
277  throw(vpException(
279  "Cannot set Euler x-y-z vector out of bounds. It has only %d values while you try to initialize with %d values",
280  size(), list.size()));
281  }
282  std::copy(list.begin(), list.end(), data);
283  return *this;
284 }
285 #endif
286 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:1107
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 & buildFrom(const vpHomogeneousMatrix &M)
Implementation of a generic rotation vector.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
vpRxyzVector & operator=(const vpColVector &rxyz)
vpRxyzVector & buildFrom(const vpRotationMatrix &R)
Implementation of a rotation vector as axis-angle minimal representation.