3dpose.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * $Id: file.cpp 3496 2011-11-22 15:14:32Z fnovotny $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2011 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Contact visp@irisa.fr if any conditions of this licensing are
34  * not clear to you.
35  *
36  * Description:
37  * conversions between ROS and ViSP structures representing a 3D pose
38  *
39  * Authors:
40  * Filip Novotny
41  *
42  *
43  *****************************************************************************/
44 
50 #include <cmath>
51 
52 #include <visp/vpConfig.h>
53 
54 #if VISP_VERSION_INT > (2<<16 | 6<<8 | 1)
55 # include <visp/vpQuaternionVector.h>
56 #else
57 # include <visp/vpRotationMatrix.h>
59 #endif
60 #include <visp/vpTranslationVector.h>
61 
62 #include "visp_bridge/3dpose.h"
63 
64 namespace visp_bridge{
65 
66 #if VISP_VERSION_INT > (2<<16 | 6<<8 | 1)
67  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Pose& pose){
68  vpHomogeneousMatrix mat;
69  vpTranslationVector vec(pose.position.x,pose.position.y,pose.position.z);
70  vpQuaternionVector q(pose.orientation.x,pose.orientation.y,pose.orientation.z,pose.orientation.w);
71  mat.buildFrom(vec,q);
72 
73  return mat;
74  }
75 
76  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Transform& trans){
77  vpHomogeneousMatrix mat;
78  vpTranslationVector vec(trans.translation.x,trans.translation.y,trans.translation.z);
79  vpQuaternionVector q(trans.rotation.x,trans.rotation.y,trans.rotation.z,trans.rotation.w);
80  mat.buildFrom(vec,q);
81 
82  return mat;
83  }
84 
85  geometry_msgs::Transform toGeometryMsgsTransform(const vpHomogeneousMatrix& mat){
86  geometry_msgs::Transform trans;
88  mat.extract(q);
89  trans.rotation.x = q.x();
90  trans.rotation.y = q.y();
91  trans.rotation.z = q.z();
92  trans.rotation.w = q.w();
93 
94 
95  trans.translation.x = mat[0][3];
96  trans.translation.y = mat[1][3];
97  trans.translation.z = mat[2][3];
98 
99  return trans;
100  }
101 
102  geometry_msgs::Pose toGeometryMsgsPose(const vpHomogeneousMatrix& mat){
103  geometry_msgs::Pose pose;
104 
105  vpThetaUVector tu(mat);
106  vpColVector u;
107  double theta;
108  tu.extract(theta, u);
109 
110  theta *= 0.5;
111 
112  double sinTheta_2 = sin(theta);
113 
114  pose.orientation.x = u[0] * sinTheta_2;
115  pose.orientation.y = u[1] * sinTheta_2;
116  pose.orientation.z = u[2] * sinTheta_2;
117  pose.orientation.w = cos(theta);
118 
119  pose.position.x = mat[0][3];
120  pose.position.y = mat[1][3];
121  pose.position.z = mat[2][3];
122 
123  return pose;
124  }
125 
126 
127 #else
128  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Transform& trans){
129  vpHomogeneousMatrix mat;
130  vpTranslationVector vec(trans.translation.x,trans.translation.y,trans.translation.z);
131  vpRotationMatrix rmat;
132 
133  double a = trans.rotation.x;
134  double b = trans.rotation.y;
135  double c = trans.rotation.z;
136  double d = trans.rotation.w;
137  rmat[0][0] = a*a+b*b-c*c-d*d;
138  rmat[0][1] = 2*b*c-2*a*d;
139  rmat[0][2] = 2*a*c+2*b*d;
140 
141  rmat[1][0] = 2*a*d+2*b*c;
142  rmat[1][1] = a*a-b*b+c*c-d*d;
143  rmat[1][2] = 2*c*d-2*a*b;
144 
145  rmat[2][0] = 2*b*d-2*a*c;
146  rmat[2][1] = 2*a*b+2*c*d;
147  rmat[2][2] = a*a-b*b-c*c+d*d;
148 
149  mat.buildFrom(vec,rmat);
150 
151  return mat;
152  }
153 
154  geometry_msgs::Transform toGeometryMsgsTransform(vpHomogeneousMatrix& mat){
155  geometry_msgs::Transform trans;
156  vpRotationMatrix rmat;
157  mat.extract(rmat);
158  vpQuaternionVector q(rmat);
159 
160  trans.rotation.x = q.x();
161  trans.rotation.y = q.y();
162  trans.rotation.z = q.z();
163  trans.rotation.w = q.w();
164 
165 
166  trans.translation.x = mat[0][3];
167  trans.translation.y = mat[1][3];
168  trans.translation.z = mat[2][3];
169 
170  return trans;
171  }
172 
173  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Pose& pose){
174  vpHomogeneousMatrix mat;
175  vpTranslationVector vec(pose.position.x,pose.position.y,pose.position.z);
176  vpRotationMatrix rmat;
177 
178  double a = pose.orientation.x;
179  double b = pose.orientation.y;
180  double c = pose.orientation.z;
181  double d = pose.orientation.w;
182  rmat[0][0] = a*a+b*b-c*c-d*d;
183  rmat[0][1] = 2*b*c-2*a*d;
184  rmat[0][2] = 2*a*c+2*b*d;
185 
186  rmat[1][0] = 2*a*d+2*b*c;
187  rmat[1][1] = a*a-b*b+c*c-d*d;
188  rmat[1][2] = 2*c*d-2*a*b;
189 
190  rmat[2][0] = 2*b*d-2*a*c;
191  rmat[2][1] = 2*a*b+2*c*d;
192  rmat[2][2] = a*a-b*b-c*c+d*d;
193 
194  mat.buildFrom(vec,rmat);
195 
196  return mat;
197  }
198 #endif
199 }
vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Transform &trans)
Converts a geometry_msgs::Transform to a ViSP homogeneous matrix (vpHomogeneousMatrix).
Definition: 3dpose.cpp:128
double y() const
returns y-component of the quaternion
Class that consider the case of a quaternion and basic operations on it.
double w() const
returns w-component of the quaternion
geometry_msgs::Pose toGeometryMsgsPose(const vpHomogeneousMatrix &mat)
Converts a ViSP homogeneous matrix (vpHomogeneousMatrix) to a geometry_msgs::Pose.
conversions between ROS and ViSP structures representing a 3D pose
double z() const
returns z-component of the quaternion
double x() const
returns x-component of the quaternion
Defines a quaternion and its basic operations.
geometry_msgs::Transform toGeometryMsgsTransform(const vpHomogeneousMatrix &mat)
Converts a ViSP homogeneous matrix (vpHomogeneousMatrix) to a geometry_msgs::Transform.


visp_bridge
Author(s): Filip Novotny
autogenerated on Sat Mar 13 2021 03:20:01