Eigen.hpp
Go to the documentation of this file.
1 /// @file Eigen.hpp
2 /// @todo convert to use template metaprgramming to define simple functions that simplify and automate the necessary conversions
3 #ifndef _VREP_EIGEN_HPP_
4 #define _VREP_EIGEN_HPP_
5 
6 #include <Eigen/Core>
7 #include <Eigen/Geometry>
8 #include <array>
9 #include "v_repLib.h"
10 
11 namespace vrepquat {
12  enum vrep_quat {
13  x,
14  y,
15  z,
16  w
17  };
18 }
19 
20 template<typename InputIterator>
21 Eigen::Quaterniond vrepToEigenQuaternion(InputIterator vrepQuat){
22 
23  // 0123
24  // vrep is ordered xyzw,
25  // eigen is ordered wxyz
26 
27  Eigen::Quaterniond quat(vrepQuat[vrepquat::w],vrepQuat[vrepquat::x],vrepQuat[vrepquat::y],vrepQuat[vrepquat::z]);
28  return quat;
29 }
30 
31 template<typename Q>
32 std::array<float,4> EigenToVrepQuaternion(const Q& q){
33  std::array<float,4> qa;
34 
35  // 0123
36  // vrep is ordered xyzw,
37  // eigen is ordered wxyz
38  qa[vrepquat::x] = q.x();
39  qa[vrepquat::y] = q.y();
40  qa[vrepquat::z] = q.z();
41  qa[vrepquat::w] = q.w();
42 
43  return qa;
44 }
45 
46 template<typename P>
47 std::array<float,3> EigenToVrepPosition(const P& p){
48  std::array<float,3> qv;
49  qv[0] = p.x();
50  qv[1] = p.y();
51  qv[2] = p.z();
52 
53  return qv;
54 }
55 
56 template<typename Input>
57 Eigen::Vector3d vrepQuatToEigenVector3dAngleAxis(Input vrepQuat){
58  Eigen::AngleAxisd ax3d(vrepToEigenQuaternion(vrepQuat));
59  return ax3d.angle()*ax3d.axis();
60 }
61 
62 template<typename Input>
63 Eigen::Vector3d eigenRotToEigenVector3dAngleAxis(Input eigenQuat){
64  Eigen::AngleAxisd ax3d(eigenQuat);
65  return ax3d.angle()*ax3d.axis();
66 }
67 
68 
69 template<typename InputIterator>
70 Eigen::Vector3d vrepToEigenVector3d(InputIterator vrepVec){
71  // vrep is ordered xyzw, eigen is ordered wxyz
72  Eigen::Vector3d vec(vrepVec[0],vrepVec[1],vrepVec[2]);
73  return vec;
74 }
75 
76 /// @see http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetJointMatrix.htm
77 Eigen::Affine3d vrepToEigenTransform(const std::array<float,12>& vrepTransform)
78 {
79 
80  // based on comparing each of the following:
81  // http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetJointMatrix.htm
82  // https://en.wikipedia.org/wiki/Row-major_order
83  // https://eigen.tuxfamily.org/dox/group__TopicStorageOrders.html
84 
85  // V-REP is by default Row-major order
86  // Eigen is by default Column Major order
87  Eigen::Map<const Eigen::Matrix<float,3,4,Eigen::RowMajor>> vmap(vrepTransform.cbegin());
88  Eigen::Affine3d eigenTransform;
89  eigenTransform.translation() = vmap.block<3,1>(0,3).cast<double>();
90  eigenTransform.matrix().block<3,3>(0,0) = vmap.block<3,3>(0,0).cast<double>();
91 
92  return eigenTransform;
93 }
94 
95 
96 std::pair<Eigen::Vector3d,Eigen::Vector3d> getAxisAngleAndTranslation(int ObjectHandle, int BaseFrameObjectHandle){
97 
98  std::array<float,3> simTipPosition;
99  //std::array<float,3> simTipOrientation;
100  std::array<float,4> simTipQuaternion;
101 
102  int ret = simGetObjectPosition(ObjectHandle, BaseFrameObjectHandle, simTipPosition.begin());
103  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("HandEyeCalibrationVrepPlugin: Could not get position"));
104  ret = simGetObjectQuaternion(ObjectHandle, BaseFrameObjectHandle, simTipQuaternion.begin());
105  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("HandEyeCalibrationVrepPlugin: Could not get quaternion"));
106 
107  auto simTipAngleAxis = vrepQuatToEigenVector3dAngleAxis(simTipQuaternion.begin());
108  auto simTipVec = vrepToEigenVector3d(simTipPosition.begin());
109 
110 
111 
112  return std::make_pair(simTipAngleAxis,simTipVec);
113 }
114 
115 
116 template<typename T>
117 void setObjectTransform(int objectHandle, int relativeToObjectHandle, T& transform){
118 
119  // get quaternion between end effector and
120  Eigen::Quaternion<typename T::Scalar> eigenQuat(transform.rotation());
121  std::array<float,4> vrepQuat = EigenToVrepQuaternion(eigenQuat);
122  int ret = simSetObjectQuaternion(objectHandle,relativeToObjectHandle,vrepQuat.begin());
123  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("setObjectTransform: Could not set Quaternion"));
124 
125  std::array<float,3> vrepPos = EigenToVrepPosition(transform.translation());
126  ret = simSetObjectPosition(objectHandle,relativeToObjectHandle,vrepPos.begin());
127  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("setObjectTransform: Could not set Quaternion"));
128 
129 }
130 
131 /// @param objectHandle The V-Rep object handle for which the transform is needed
132 /// @param relativeToObjectHandle The frame of reference in which to get the object handle, -1 (the default) gets the absolute position aka world frame
133 /// @see http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetObjectPosition.htm
134 /// @see http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetObjectQuaternion.htm
135 Eigen::Affine3d getObjectTransform(int objectHandle, int relativeToObjectHandle = -1){
136 
137  std::array<float,4> vrepQuat;
138  int ret = simGetObjectQuaternion(objectHandle,relativeToObjectHandle,vrepQuat.begin());
139  Eigen::Quaterniond eigenQuat(vrepToEigenQuaternion(vrepQuat));
140  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("getObjectTransform: Could not get Quaternion"));
141 
142  std::array<float,3> vrepPos;
143  ret = simGetObjectPosition(objectHandle,relativeToObjectHandle,vrepPos.begin());
144  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("getObjectTransform: Could not get Position"));
145  Eigen::Vector3d eigenPos(vrepToEigenVector3d(vrepPos));
146 
147  Eigen::Affine3d transform;
148 
149  transform = eigenQuat;
150  transform.translation() = eigenPos;
151 
152  return transform;
153 }
154 
155 /// @param objectHandle The V-Rep object handle for which the transform is needed
156 /// @param relativeToObjectHandle The frame of reference in which to get the object handle, -1 (the default) gets the absolute position aka world frame
157 /// @see http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetObjectPosition.htm
158 /// @see http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetObjectQuaternion.htm
159 /// @todo TODO(ahundt) handle errors/return codes from simGetObjectPosition
160 std::pair<Eigen::Quaterniond,Eigen::Vector3d> getObjectTransformQuaternionTranslationPair(int objectHandle, int relativeToObjectHandle = -1){
161 
162  std::array<float,4> vrepQuat;
163  int ret = simGetObjectQuaternion(objectHandle,relativeToObjectHandle,vrepQuat.begin());
164  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("getObjectTransformQuaternionTranslationPair: Could not get Quaternion"));
165  Eigen::Quaterniond eigenQuat(vrepToEigenQuaternion(vrepQuat));
166 
167  std::array<float,3> vrepPos;
168  ret = simGetObjectPosition(objectHandle,relativeToObjectHandle,vrepPos.begin());
169  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("getObjectTransformQuaternionTranslationPair: Could not get Position"));
170  Eigen::Vector3d eigenPos(vrepToEigenVector3d(vrepPos));
171 
172  return std::make_pair(eigenQuat,eigenPos);
173 }
174 
175 /// Eigen version of simGetJointMatrix
176 Eigen::Affine3d getJointTransform(int objectHandle)
177 {
178  // based on comparing each of the following:
179  // http://www.coppeliarobotics.com/helpFiles/en/regularApi/simGetJointMatrix.htm
180  // https://en.wikipedia.org/wiki/Row-major_order
181  // https://eigen.tuxfamily.org/dox/group__TopicStorageOrders.html
182  std::array<float,12> vrepTransform;
183  int ret = simGetJointMatrix(objectHandle,vrepTransform.begin());
184  if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error("getJointTransform: Could not get Joint Matrix"));
185  return vrepToEigenTransform(vrepTransform);
186 }
187 
188 template<typename T>
189 static std::string poseString(const Eigen::Transform<T,3,Eigen::Affine>& pose, const std::string& pfx = "")
190 {
191  std::stringstream ss;
192  ss.precision(3);
193  for (int y=0;y<4;y++)
194  {
195  ss << pfx;
196  for (int x=0;x<4;x++)
197  {
198  ss << std::setw(8) << pose(y,x) << " ";
199  }
200  ss << std::endl;
201  }
202  return ss.str();
203 }
204 
205 
206 //---------
207 //posString
208 //---------
209 static std::string posString(const Eigen::Vector3d& pos, const std::string& pfx = "(", const std::string& sfx = ")")
210 {
211  std::stringstream ss;
212  ss.precision(3);
213  ss << pfx;
214  for (int x=0;x<3;x++)
215  {
216  if (x)
217  ss << ", ";
218  ss << std::setw(8) << pos(x);
219  }
220  ss << sfx;
221  return ss.str();
222 }
223 
224 #endif
vrep_quat
Definition: Eigen.hpp:12
ptrSimGetJointMatrix simGetJointMatrix
Definition: v_repLib.cpp:87
Eigen::Affine3d getObjectTransform(int objectHandle, int relativeToObjectHandle=-1)
Definition: Eigen.hpp:135
Eigen::Vector3d vrepQuatToEigenVector3dAngleAxis(Input vrepQuat)
Definition: Eigen.hpp:57
Eigen::Vector3d vrepToEigenVector3d(InputIterator vrepVec)
Definition: Eigen.hpp:70
ptrSimGetObjectPosition simGetObjectPosition
Definition: v_repLib.cpp:75
std::array< float, 3 > EigenToVrepPosition(const P &p)
Definition: Eigen.hpp:47
Eigen::Affine3d vrepToEigenTransform(const std::array< float, 12 > &vrepTransform)
Definition: Eigen.hpp:77
ptrSimSetObjectPosition simSetObjectPosition
Definition: v_repLib.cpp:76
Eigen::Vector3d eigenRotToEigenVector3dAngleAxis(Input eigenQuat)
Definition: Eigen.hpp:63
Eigen::Affine3d getJointTransform(int objectHandle)
Eigen version of simGetJointMatrix.
Definition: Eigen.hpp:176
void setObjectTransform(int objectHandle, int relativeToObjectHandle, T &transform)
Definition: Eigen.hpp:117
ptrSimSetObjectQuaternion simSetObjectQuaternion
Definition: v_repLib.cpp:365
ptrSimGetObjectQuaternion simGetObjectQuaternion
Definition: v_repLib.cpp:364
std::pair< Eigen::Vector3d, Eigen::Vector3d > getAxisAngleAndTranslation(int ObjectHandle, int BaseFrameObjectHandle)
Definition: Eigen.hpp:96
std::array< float, 4 > EigenToVrepQuaternion(const Q &q)
Definition: Eigen.hpp:32
Eigen::Quaterniond vrepToEigenQuaternion(InputIterator vrepQuat)
Definition: Eigen.hpp:21
std::pair< Eigen::Quaterniond, Eigen::Vector3d > getObjectTransformQuaternionTranslationPair(int objectHandle, int relativeToObjectHandle=-1)
Definition: Eigen.hpp:160