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 > VP_VERSION_INT(2,6,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 
72 #if VISP_VERSION_INT > VP_VERSION_INT(3,6,0)
73  mat.build(vec,q);
74 #else
75  mat.buildFrom(vec,q);
76 #endif
77 
78  return mat;
79  }
80 
81  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Transform& trans){
82  vpHomogeneousMatrix mat;
83  vpTranslationVector vec(trans.translation.x,trans.translation.y,trans.translation.z);
84  vpQuaternionVector q(trans.rotation.x,trans.rotation.y,trans.rotation.z,trans.rotation.w);
85 #if VISP_VERSION_INT > VP_VERSION_INT(3,6,0)
86  mat.build(vec,q);
87 #else
88  mat.buildFrom(vec,q);
89 #endif
90 
91  return mat;
92  }
93 
94  geometry_msgs::Transform toGeometryMsgsTransform(const vpHomogeneousMatrix& mat){
95  geometry_msgs::Transform trans;
97  mat.extract(q);
98  trans.rotation.x = q.x();
99  trans.rotation.y = q.y();
100  trans.rotation.z = q.z();
101  trans.rotation.w = q.w();
102 
103 
104  trans.translation.x = mat[0][3];
105  trans.translation.y = mat[1][3];
106  trans.translation.z = mat[2][3];
107 
108  return trans;
109  }
110 
111  geometry_msgs::Pose toGeometryMsgsPose(const vpHomogeneousMatrix& mat){
112  geometry_msgs::Pose pose;
113 
114  vpThetaUVector tu(mat);
115  vpColVector u;
116  double theta;
117  tu.extract(theta, u);
118 
119  theta *= 0.5;
120 
121  double sinTheta_2 = sin(theta);
122 
123  pose.orientation.x = u[0] * sinTheta_2;
124  pose.orientation.y = u[1] * sinTheta_2;
125  pose.orientation.z = u[2] * sinTheta_2;
126  pose.orientation.w = cos(theta);
127 
128  pose.position.x = mat[0][3];
129  pose.position.y = mat[1][3];
130  pose.position.z = mat[2][3];
131 
132  return pose;
133  }
134 
135 
136 #else
137  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Transform& trans){
138  vpHomogeneousMatrix mat;
139  vpTranslationVector vec(trans.translation.x,trans.translation.y,trans.translation.z);
140  vpRotationMatrix rmat;
141 
142  double a = trans.rotation.x;
143  double b = trans.rotation.y;
144  double c = trans.rotation.z;
145  double d = trans.rotation.w;
146  rmat[0][0] = a*a+b*b-c*c-d*d;
147  rmat[0][1] = 2*b*c-2*a*d;
148  rmat[0][2] = 2*a*c+2*b*d;
149 
150  rmat[1][0] = 2*a*d+2*b*c;
151  rmat[1][1] = a*a-b*b+c*c-d*d;
152  rmat[1][2] = 2*c*d-2*a*b;
153 
154  rmat[2][0] = 2*b*d-2*a*c;
155  rmat[2][1] = 2*a*b+2*c*d;
156  rmat[2][2] = a*a-b*b-c*c+d*d;
157 
158 #if VISP_VERSION_INT > VP_VERSION_INT(3,6,0)
159  mat.build(vec,rmat);
160 #else
161  mat.buildFrom(vec,rmat);
162 #endif
163 
164  return mat;
165  }
166 
167  geometry_msgs::Transform toGeometryMsgsTransform(vpHomogeneousMatrix& mat){
168  geometry_msgs::Transform trans;
169  vpRotationMatrix rmat;
170  mat.extract(rmat);
171  vpQuaternionVector q(rmat);
172 
173  trans.rotation.x = q.x();
174  trans.rotation.y = q.y();
175  trans.rotation.z = q.z();
176  trans.rotation.w = q.w();
177 
178 
179  trans.translation.x = mat[0][3];
180  trans.translation.y = mat[1][3];
181  trans.translation.z = mat[2][3];
182 
183  return trans;
184  }
185 
186  vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Pose& pose){
187  vpHomogeneousMatrix mat;
188  vpTranslationVector vec(pose.position.x,pose.position.y,pose.position.z);
189  vpRotationMatrix rmat;
190 
191  double a = pose.orientation.x;
192  double b = pose.orientation.y;
193  double c = pose.orientation.z;
194  double d = pose.orientation.w;
195  rmat[0][0] = a*a+b*b-c*c-d*d;
196  rmat[0][1] = 2*b*c-2*a*d;
197  rmat[0][2] = 2*a*c+2*b*d;
198 
199  rmat[1][0] = 2*a*d+2*b*c;
200  rmat[1][1] = a*a-b*b+c*c-d*d;
201  rmat[1][2] = 2*c*d-2*a*b;
202 
203  rmat[2][0] = 2*b*d-2*a*c;
204  rmat[2][1] = 2*a*b+2*c*d;
205  rmat[2][2] = a*a-b*b-c*c+d*d;
206 
207 #if VISP_VERSION_INT > VP_VERSION_INT(3,6,0)
208  mat.build(vec,rmat);
209 #else
210  mat.buildFrom(vec,rmat);
211 #endif
212 
213  return mat;
214  }
215 #endif
216 }
3dpose.h
conversions between ROS and ViSP structures representing a 3D pose
vpQuaternionVector::x
double x() const
returns x-component of the quaternion
Definition: vpQuaternionVector.h:97
vpQuaternionVector::w
double w() const
returns w-component of the quaternion
Definition: vpQuaternionVector.h:103
visp_bridge::toGeometryMsgsTransform
geometry_msgs::Transform toGeometryMsgsTransform(const vpHomogeneousMatrix &mat)
Converts a ViSP homogeneous matrix (vpHomogeneousMatrix) to a geometry_msgs::Transform.
vpQuaternionVector::y
double y() const
returns y-component of the quaternion
Definition: vpQuaternionVector.h:99
visp_bridge::toVispHomogeneousMatrix
vpHomogeneousMatrix toVispHomogeneousMatrix(const geometry_msgs::Transform &trans)
Converts a geometry_msgs::Transform to a ViSP homogeneous matrix (vpHomogeneousMatrix).
Definition: 3dpose.cpp:137
visp_bridge
Definition: 3dpose.h:59
d
d
vpQuaternionVector
Defines a quaternion and its basic operations.
Definition: vpQuaternionVector.h:80
visp_bridge::toGeometryMsgsPose
geometry_msgs::Pose toGeometryMsgsPose(const vpHomogeneousMatrix &mat)
Converts a ViSP homogeneous matrix (vpHomogeneousMatrix) to a geometry_msgs::Pose.
vpQuaternionVector::z
double z() const
returns z-component of the quaternion
Definition: vpQuaternionVector.h:101
vpQuaternionVector.h
Class that consider the case of a quaternion and basic operations on it.


visp_bridge
Author(s): Filip Novotny
autogenerated on Sat Aug 24 2024 02:54:51