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 
14 #include <cstdio>
15 
16 namespace {
17 YARP_LOG_COMPONENT(FRAMETRANSFORM, "yarp.math.FrameTransform")
18 }
19 
21  timestamp(0)
22 {
23  translation.set(0, 0, 0);
24 }
25 
26 yarp::math::FrameTransform::FrameTransform (const std::string& parent,
27  const std::string& child,
28  double inTX,
29  double inTY,
30  double inTZ,
31  double inRX,
32  double inRY,
33  double inRZ,
34  double inRW) :
35  src_frame_id(parent),
36  dst_frame_id(child),
37  translation{inTX, inTY, inTZ},
38  rotation(inRX, inRY, inRZ, inRW)
39 {
40 }
41 
43 
44 void yarp::math::FrameTransform::transFromVec(double X, double Y, double Z)
45 {
46  translation.set(X, Y, Z);
47 }
48 
49 void yarp::math::FrameTransform::rotFromRPY(double R, double P, double Y)
50 {
51  double rot[3] = { R, P, Y };
52  size_t i = 3;
53  yarp::sig::Vector rotV;
54  yarp::sig::Matrix rotM;
55  rotV = yarp::sig::Vector(i, rot);
56  rotM = rpy2dcm(rotV);
57  rotation.fromRotationMatrix(rotM);
58 }
59 
61 {
62  yarp::sig::Vector rotV;
63  yarp::sig::Matrix rotM;
64  rotM = rotation.toRotationMatrix4x4();
65  rotV = dcm2rpy(rotM);
66  return rotV;
67 }
68 
70 {
71  yarp::sig::Vector rotV;
72  yarp::sig::Matrix t_mat(4,4);
73  t_mat = rotation.toRotationMatrix4x4();
74  t_mat[0][3] = translation.tX;
75  t_mat[1][3] = translation.tY;
76  t_mat[2][3] = translation.tZ;
77  return t_mat;
78 }
79 
81 {
82  if (mat.cols() != 4 || mat.rows() != 4)
83  {
84  yCError(FRAMETRANSFORM, "FrameTransform::fromMatrix() failed, matrix should be = 4x4");
85  yCAssert(FRAMETRANSFORM, mat.cols() == 4 && mat.rows() == 4);
86  return false;
87  }
88 
90 
91  translation.tX = mat[0][3];
92  translation.tY = mat[1][3];
93  translation.tZ = mat[2][3];
94  rotation.fromRotationMatrix(mat);
95  return true;
96 }
97 
98 
99 
101 {
102  char buff[1024];
103  sprintf(buff, "%s -> %s \n tran: %f %f %f \n rot: %f %f %f %f \n\n",
104  src_frame_id.c_str(),
105  dst_frame_id.c_str(),
106  translation.tX,
107  translation.tY,
108  translation.tZ,
109  rotation.x(),
110  rotation.y(),
111  rotation.z(),
112  rotation.w());
113  return std::string(buff);
114 }
void rotFromRPY(double R, double P, double Y)
bool fromMatrix(const yarp::sig::Matrix &mat)
struct yarp::math::FrameTransform::Translation_t translation
void transFromVec(double X, double Y, double Z)
yarp::sig::Matrix toMatrix() const
yarp::sig::Vector getRPYRot() const
std::string toString() const
A class for a Matrix.
Definition: Matrix.h:46
size_t cols() const
Return number of columns.
Definition: Matrix.h:101
size_t rows() const
Return number of rows.
Definition: Matrix.h:95
#define yCError(component,...)
Definition: LogComponent.h:157
#define yCAssert(component, x)
Definition: LogComponent.h:172
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
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::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
VectorOf< double > Vector
Definition: Vector.h:33
void set(double x, double y, double z)