choreo_geometry_conversion_helpers.cpp
Go to the documentation of this file.
2 
3 // mesh collision geometry import
6 
7 #include <boost/filesystem.hpp>
8 
9 namespace{
10 
12  const std::vector<geometry_msgs::Vector3>& orients_msg,
13  std::vector<Eigen::Matrix3d>& m_orients)
14 {
15  m_orients.clear();
16 
17  for(auto v : orients_msg)
18  {
19  // eigen_vec = local z axis
20  Eigen::Vector3d eigen_vec;
21  tf::vectorMsgToEigen(v, eigen_vec);
22 
23  // TODO: this should be removed
24  eigen_vec *= -1.0;
25  eigen_vec.normalize();
26 
27  // construct local x axis & y axis
28  Eigen::Vector3d candidate_dir = Eigen::Vector3d::UnitX();
29  if ( std::abs(eigen_vec.dot(Eigen::Vector3d::UnitX())) > 0.8 )
30  {
31  // if z axis = UnitX,
32  candidate_dir = Eigen::Vector3d::UnitY();
33  }
34 
35  Eigen::Vector3d y_vec = eigen_vec.cross(candidate_dir).normalized();
36 
37  Eigen::Vector3d x_vec = y_vec.cross(eigen_vec).normalized();
38 
39  // JM
40  Eigen::Matrix3d m = Eigen::Matrix3d::Identity();
41  m.col(0) = x_vec;
42  m.col(1) = y_vec;
43  m.col(2) = eigen_vec;
44 
45  m_orients.push_back(m);
46  }
47 }
48 
49 void planeAxesToEigenMatrixImpl(const Eigen::Vector3d& x_axis, const Eigen::Vector3d& y_axis, const Eigen::Vector3d& z_axis,
50  Eigen::Matrix3d& m)
51 {
52  m = Eigen::Matrix3d::Identity();
53  m.col(0) = x_axis;
54  m.col(1) = y_axis;
55  m.col(2) = z_axis;
56 }
57 
58 void planeToEigenAffine3dImpl(const Eigen::Vector3d& o, const Eigen::Matrix3d& m, Eigen::Affine3d& e)
59 {
60  e = Eigen::Affine3d::Identity();
61  e.matrix().block<3,3>(0,0) = m;
62  e.matrix().col(3).head<3>() = o;
63 }
64 
65 } // anon util namespace
66 
68 {
69 const static std::string FILE_URL_PREFIX = "file://";
70 
71 void planeAxesToEigenMatrix(const Eigen::Vector3d& x_axis, const Eigen::Vector3d& y_axis, const Eigen::Vector3d& z_axis,
72  Eigen::Matrix3d& m)
73 {
74  planeAxesToEigenMatrixImpl(x_axis, y_axis, z_axis, m);
75 }
76 
77 void planeAxesToQuaternionMsg(const Eigen::Vector3d& x_axis, const Eigen::Vector3d& y_axis, const Eigen::Vector3d& z_axis,
78  geometry_msgs::Quaternion& q_msg)
79 {
80  Eigen::Matrix3d m;
81  planeAxesToEigenMatrixImpl(x_axis, y_axis, z_axis, m);
82 
83  Eigen::Quaterniond e_q(m);
84  tf::quaternionEigenToMsg(e_q, q_msg);
85 }
86 
87 void planeToEigenAffine3d(const Eigen::Vector3d& origin,
88  const Eigen::Vector3d& x_axis, const Eigen::Vector3d& y_axis, const Eigen::Vector3d& z_axis,
89  Eigen::Affine3d& e)
90 {
91  Eigen::Matrix3d m;
92  planeAxesToEigenMatrixImpl(x_axis, y_axis, z_axis, m);
93 
94  planeToEigenAffine3dImpl(origin, m, e);
95 }
96 
97 void planeToEigenAffine3d(const Eigen::Vector3d& origin, const Eigen::Matrix3d& orientation, Eigen::Affine3d& e)
98 {
99  planeToEigenAffine3dImpl(origin, orientation, e);
100 }
101 
102 void planeToPoseMsg(const Eigen::Vector3d& origin,
103  const Eigen::Vector3d& x_axis, const Eigen::Vector3d& y_axis, const Eigen::Vector3d& z_axis,
104  geometry_msgs::Pose& p)
105 {
106  Eigen::Affine3d e;
107 
108  Eigen::Matrix3d m;
109  planeAxesToEigenMatrixImpl(x_axis, y_axis, z_axis, m);
110 
111  planeToEigenAffine3dImpl(origin, m, e);
112  tf::poseEigenToMsg(e, p);
113 }
114 
115 void planeToPoseMsg(const Eigen::Vector3d& origin, const Eigen::Matrix3d& orientation,
116  geometry_msgs::Pose& p)
117 {
118  Eigen::Affine3d e;
119  planeToEigenAffine3dImpl(origin, orientation, e);
120  tf::poseEigenToMsg(e, p);
121 }
122 
123 void savedSTLToMeshShapeMsg(const std::string& file_path, const Eigen::Vector3d& scale_vector, shape_msgs::Mesh& mesh)
124 {
125  assert(boost::filesystem::exists(file_path));
126 
127  shapes::Mesh* m = shapes::createMeshFromResource(FILE_URL_PREFIX + file_path, scale_vector);
128 
129  shapes::ShapeMsg mesh_msg;
130  shapes::constructMsgFromShape(m, mesh_msg);
131 
132  mesh = boost::get<shape_msgs::Mesh>(mesh_msg);
133 }
134 
135 moveit_msgs::CollisionObject savedSTLToCollisionObjectMsg(const std::string& file_path,
136  const Eigen::Vector3d& scale_vector,
137  const std::string& frame_id,
138  const geometry_msgs::Pose& p,
139  int object_operation)
140 {
141  moveit_msgs::CollisionObject co;
142 
143  savedSTLToCollisionObjectMsg(file_path, scale_vector, frame_id, p, co, object_operation);
144 
145  return co;
146 }
147 
148 void savedSTLToCollisionObjectMsg(const std::string& file_path,
149  const Eigen::Vector3d& scale_vector,
150  const std::string& frame_id,
151  const geometry_msgs::Pose& p,
152  moveit_msgs::CollisionObject& co,
153  int object_operation)
154 {
155  assert(boost::filesystem::exists(file_path));
156 
157  boost::filesystem::path boost_path(file_path);
158 
159  savedSTLToCollisionObjectMsg(file_path, scale_vector, frame_id,
160  boost_path.filename().string(),
161  p, co, object_operation);
162 }
163 
164 void savedSTLToCollisionObjectMsg(const std::string& file_path,
165  const Eigen::Vector3d& scale_vector,
166  const std::string& frame_id,
167  const std::string& obj_id,
168  const geometry_msgs::Pose& p,
169  moveit_msgs::CollisionObject& co,
170  int op)
171 {
172  assert(boost::filesystem::exists(file_path));
173 
174  co.header.frame_id = frame_id;
175  co.id = obj_id;
176 
177  co.meshes.resize(1);
178  co.mesh_poses.resize(1);
179 
180  savedSTLToMeshShapeMsg(file_path, scale_vector, co.meshes[0]);
181  co.mesh_poses[0] = p;
182 
183  assert(co.ADD == op || co.REMOVE == op || co.APPEND == op || co.MOVE == op);
184  co.operation = op;
185 }
186 
187 moveit_msgs::AttachedCollisionObject savedSTLToAttachedCollisionObjectMsg(
188  const std::string& file_path,
189  const Eigen::Vector3d& scale_vector,
190  const std::string& frame_id,
191  const std::string& link_id,
192  const geometry_msgs::Pose& p,
193  int object_operation)
194 {
195  moveit_msgs::AttachedCollisionObject ato;
196 
197  savedSTLToAttachedCollisionObjectMsg(file_path, scale_vector, frame_id, link_id, p, ato, object_operation);
198 
199  return ato;
200 }
201 
202 void savedSTLToAttachedCollisionObjectMsg(const std::string& file_path,
203  const Eigen::Vector3d& scale_vector,
204  const std::string& frame_id,
205  const std::string& link_id,
206  const geometry_msgs::Pose& p,
207  moveit_msgs::AttachedCollisionObject& aco,
208  int object_operation)
209 {
210  assert(boost::filesystem::exists(file_path));
211 
212  boost::filesystem::path boost_path(file_path);
213 
214  savedSTLToAttachedCollisionObjectMsg(file_path, scale_vector, frame_id, link_id,
215  boost_path.filename().string(),
216  p, aco, object_operation);
217 }
218 
219 void savedSTLToAttachedCollisionObjectMsg(const std::string& file_path,
220  const Eigen::Vector3d& scale_vector,
221  const std::string& frame_id,
222  const std::string& link_id,
223  const std::string& obj_id,
224  const geometry_msgs::Pose& p,
225  moveit_msgs::AttachedCollisionObject& aco,
226  int object_operation)
227 {
228  aco.link_name = link_id;
229 
230  // use default touch link
231 
232  savedSTLToCollisionObjectMsg(file_path, scale_vector, frame_id, obj_id, p, aco.object, object_operation);
233 }
234 
235 }
moveit_msgs::AttachedCollisionObject savedSTLToAttachedCollisionObjectMsg(const std::string &file_path, const Eigen::Vector3d &scale_vector, const std::string &frame_id, const std::string &link_id, const geometry_msgs::Pose &p, int object_operation=moveit_msgs::CollisionObject::ADD)
void poseEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Pose &m)
void quaternionEigenToMsg(const Eigen::Quaterniond &e, geometry_msgs::Quaternion &m)
void convertOrientationVectors(const std::vector< geometry_msgs::Vector3 > &orients_msg, std::vector< Eigen::Matrix3d > &m_orients)
void savedSTLToCollisionObjectMsg(const std::string &file_path, const Eigen::Vector3d &scale_vector, const std::string &frame_id, const geometry_msgs::Pose &p, moveit_msgs::CollisionObject &co, int object_operation=moveit_msgs::CollisionObject::ADD)
void vectorMsgToEigen(const geometry_msgs::Vector3 &m, Eigen::Vector3d &e)
void savedSTLToMeshShapeMsg(const std::string &file_path, const Eigen::Vector3d &scale_vector, shape_msgs::Mesh &mesh)
void planeToEigenAffine3d(const Eigen::Vector3d &origin, const Eigen::Vector3d &x_axis, const Eigen::Vector3d &y_axis, const Eigen::Vector3d &z_axis, Eigen::Affine3d &e)
void planeAxesToEigenMatrix(const Eigen::Vector3d &x_axis, const Eigen::Vector3d &y_axis, const Eigen::Vector3d &z_axis, Eigen::Matrix3d &m)
void planeAxesToQuaternionMsg(const Eigen::Vector3d &x_axis, const Eigen::Vector3d &y_axis, const Eigen::Vector3d &z_axis, geometry_msgs::Quaternion &q_msg)
bool constructMsgFromShape(const Shape *shape, ShapeMsg &shape_msg)
Mesh * createMeshFromResource(const std::string &resource)
void planeToPoseMsg(const Eigen::Vector3d &origin, const Eigen::Vector3d &x_axis, const Eigen::Vector3d &y_axis, const Eigen::Vector3d &z_axis, geometry_msgs::Pose &p)
boost::variant< shape_msgs::SolidPrimitive, shape_msgs::Mesh, shape_msgs::Plane > ShapeMsg


choreo_geometry_conversion_helpers
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:58:54