YARP
Yet Another Robot Platform
FrameTransform.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <yarp/math/Math.h>
12 #include <yarp/os/LogComponent.h>
13 #include <yarp/os/LogStream.h>
14 #include <cstdio>
15 #include <cmath>
16 
17 namespace {
18 YARP_LOG_COMPONENT(FRAMETRANSFORM, "yarp.math.FrameTransform")
19 }
20 
22  timestamp(0)
23 {
24  translation.set(0, 0, 0);
25 }
26 
27 yarp::math::FrameTransform::FrameTransform (const std::string& parent,
28  const std::string& child,
29  double inTX,
30  double inTY,
31  double inTZ,
32  double inRX,
33  double inRY,
34  double inRZ,
35  double inRW) :
36  src_frame_id(parent),
37  dst_frame_id(child),
38  translation{inTX, inTY, inTZ},
39  rotation(inRX, inRY, inRZ, inRW)
40 {
41 }
42 
44 
45 void yarp::math::FrameTransform::transFromVec(double X, double Y, double Z)
46 {
47  translation.set(X, Y, Z);
48 }
49 
50 void yarp::math::FrameTransform::rotFromRPY(double R, double P, double Y)
51 {
52  double rot[3] = { R, P, Y };
53  size_t i = 3;
54  yarp::sig::Vector rotV;
55  yarp::sig::Matrix rotM;
56  rotV = yarp::sig::Vector(i, rot);
57  rotM = rpy2dcm(rotV);
58  //yCDebug(FRAMETRANSFORM) << rotM.toString();
59  rotation.fromRotationMatrix(rotM);
60 }
61 
63 {
64  yarp::sig::Vector rotV;
65  yarp::sig::Matrix rotM;
66  rotM = rotation.toRotationMatrix4x4();
67  rotV = dcm2rpy(rotM);
68  return rotV;
69 }
70 
72 {
73  yarp::sig::Vector rotV;
74  yarp::sig::Matrix t_mat(4,4);
75  t_mat = rotation.toRotationMatrix4x4();
76  t_mat[0][3] = translation.tX;
77  t_mat[1][3] = translation.tY;
78  t_mat[2][3] = translation.tZ;
79  return t_mat;
80 }
81 
83 {
84  if (mat.cols() != 4 || mat.rows() != 4)
85  {
86  yCError(FRAMETRANSFORM, "FrameTransform::fromMatrix() failed, matrix should be = 4x4");
87  yCAssert(FRAMETRANSFORM, mat.cols() == 4 && mat.rows() == 4);
88  return false;
89  }
90 
92 
93  translation.tX = mat[0][3];
94  translation.tY = mat[1][3];
95  translation.tZ = mat[2][3];
96  rotation.fromRotationMatrix(mat);
97  return true;
98 }
99 
100 
101 
103 {
104  char buff[1024];
105 
106  if (format == rotation_as_quaternion)
107  {
108  sprintf(buff, "%s -> %s \n tran: %f %f %f \n rot quaternion: %f %f %f %f\n\n",
109  src_frame_id.c_str(),
110  dst_frame_id.c_str(),
111  translation.tX,
112  translation.tY,
113  translation.tZ,
114  rotation.x(),
115  rotation.y(),
116  rotation.z(),
117  rotation.w());
118  /*
119  Quaternion normrotation= rotation;
120  normrotation.normalize();
121  sprintf(buff, "%s -> %s \n tran: %f %f %f \n rot norm quaternion: %f %f %f %f\n\n",
122  src_frame_id.c_str(),
123  dst_frame_id.c_str(),
124  translation.tX,
125  translation.tY,
126  translation.tZ,
127  normrotation.x(),
128  normrotation.y(),
129  normrotation.z(),
130  normrotation.w());
131  */
132  }
133  else if (format == rotation_as_matrix)
134  {
135  yarp::sig::Matrix rotM;
136  rotM = rotation.toRotationMatrix4x4();
137  rotM[0][3] = translation.tX;
138  rotM[1][3] = translation.tY;
139  rotM[2][3] = translation.tZ;
140  std::string s_rotm =rotM.toString();
141  sprintf(buff, "%s -> %s \n transformation matrix:\n %s \n\n",
142  src_frame_id.c_str(),
143  dst_frame_id.c_str(),
144  s_rotm.c_str());
145  }
146  else if (format == rotation_as_rpy)
147  {
148  yarp::sig::Vector rotVrad;
149  yarp::sig::Matrix rotM;
150  rotM = rotation.toRotationMatrix3x3();
151  //yCDebug(FRAMETRANSFORM)<< rotM.toString();
152  rotVrad = dcm2rpy(rotM);
153  yarp::sig::Vector rotVdeg = rotVrad*180/M_PI;
154  std::string s_rotmr = rotVrad.toString();
155  std::string s_rotmd = rotVdeg.toString();
156  sprintf(buff, "%s -> %s \n tran: %f %f %f \n rotation rpy: %s (deg %s)\n\n",
157  src_frame_id.c_str(),
158  dst_frame_id.c_str(),
159  translation.tX,
160  translation.tY,
161  translation.tZ,
162  s_rotmr.c_str(),
163  s_rotmd.c_str());
164  }
165  return std::string(buff);
166 }
LogStream.h
yarp::sig::Matrix::toString
std::string toString(int precision=-1, int width=-1, const char *endRowStr="\n") const
Print matrix to a string.
Definition: Matrix.cpp:167
yarp::math::FrameTransform::~FrameTransform
~FrameTransform()
M_PI
#define M_PI
Definition: fakeLocalizerDev.cpp:44
yarp::math::dcm2rpy
yarp::sig::Vector dcm2rpy(const yarp::sig::Matrix &R)
Converts a dcm (direction cosine matrix) rotation matrix to roll-pitch-yaw angles (defined in Math....
Definition: math.cpp:780
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::sig::Matrix::rows
size_t rows() const
Return number of rows.
Definition: Matrix.h:95
yarp::math::FrameTransform::fromMatrix
bool fromMatrix(const yarp::sig::Matrix &mat)
Definition: FrameTransform.cpp:82
yarp::math::rpy2dcm
yarp::sig::Matrix rpy2dcm(const yarp::sig::Vector &rpy)
Converts roll-pitch-yaw angles in the corresponding dcm (direction cosine matrix) rotation matrix (de...
Definition: math.cpp:818
yarp::sig::VectorOf< double >
yarp::math::FrameTransform::translation
struct yarp::math::FrameTransform::Translation_t translation
yarp::math::FrameTransform::transFromVec
void transFromVec(double X, double Y, double Z)
Definition: FrameTransform.cpp:45
yarp::math::FrameTransform::toString
std::string toString(display_transform_mode_t format=rotation_as_quaternion) const
Definition: FrameTransform.cpp:102
Math.h
yarp::math::FrameTransform::rotFromRPY
void rotFromRPY(double R, double P, double Y)
Definition: FrameTransform.cpp:50
yarp::math::FrameTransform::getRPYRot
yarp::sig::Vector getRPYRot() const
Definition: FrameTransform.cpp:62
yarp::sig::Matrix::cols
size_t cols() const
Return number of columns.
Definition: Matrix.h:101
LogComponent.h
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::sig::VectorOf::toString
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition: Vector.h:391
yarp::sig::Vector
VectorOf< double > Vector
Definition: Vector.h:33
yarp::math::FrameTransform::toMatrix
yarp::sig::Matrix toMatrix() const
Definition: FrameTransform.cpp:71
FrameTransform.h
yarp::math::FrameTransform::display_transform_mode_t
display_transform_mode_t
Definition: FrameTransform.h:65
yarp::math::FrameTransform::FrameTransform
FrameTransform()
Definition: FrameTransform.cpp:21
yarp::math::FrameTransform::Translation_t::set
void set(double x, double y, double z)
Definition: FrameTransform.h:33
yarp::sig::Matrix
A class for a Matrix.
Definition: Matrix.h:46