moveit2
The MoveIt Motion Planning Framework for ROS 2.
ros_bullet_utils.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD-2-Clause)
3  *
4  * Copyright (c) 2021, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *********************************************************************/
31 
32 /* Author: Jorge Nicho*/
33 
35 
36 #include <rclcpp/logger.hpp>
37 #include <rclcpp/logging.hpp>
38 
39 const rclcpp::Logger BULLET_LOGGER = rclcpp::get_logger("collision_detection.bullet");
40 
42 {
43 void getActiveLinkNamesRecursive(std::vector<std::string>& active_links, const urdf::LinkConstSharedPtr& urdf_link,
44  bool active)
45 {
46  if (active)
47  {
48  active_links.push_back(urdf_link->name);
49  for (const auto& child_link : urdf_link->child_links)
50  {
51  getActiveLinkNamesRecursive(active_links, child_link, active);
52  }
53  }
54  else
55  {
56  for (std::size_t i = 0; i < urdf_link->child_links.size(); ++i)
57  {
58  const urdf::LinkConstSharedPtr child_link = urdf_link->child_links[i];
59  if ((child_link->parent_joint) && (child_link->parent_joint->type != urdf::Joint::FIXED))
60  getActiveLinkNamesRecursive(active_links, child_link, true);
61  else
62  getActiveLinkNamesRecursive(active_links, child_link, active);
63  }
64  }
65 }
66 
67 shapes::ShapePtr constructShape(const urdf::Geometry* geom)
68 {
69  shapes::Shape* result = nullptr;
70  switch (geom->type)
71  {
72  case urdf::Geometry::SPHERE:
73  result = new shapes::Sphere(static_cast<const urdf::Sphere*>(geom)->radius);
74  break;
75  case urdf::Geometry::BOX:
76  {
77  urdf::Vector3 dim = static_cast<const urdf::Box*>(geom)->dim;
78  result = new shapes::Box(dim.x, dim.y, dim.z);
79  }
80  break;
81  case urdf::Geometry::CYLINDER:
82  result = new shapes::Cylinder(static_cast<const urdf::Cylinder*>(geom)->radius,
83  static_cast<const urdf::Cylinder*>(geom)->length);
84  break;
85  case urdf::Geometry::MESH:
86  {
87  const urdf::Mesh* mesh = static_cast<const urdf::Mesh*>(geom);
88  if (!mesh->filename.empty())
89  {
90  Eigen::Vector3d scale(mesh->scale.x, mesh->scale.y, mesh->scale.z);
91  shapes::Mesh* m = shapes::createMeshFromResource(mesh->filename, scale);
92  result = m;
93  }
94  }
95  break;
96  default:
97  RCLCPP_ERROR(BULLET_LOGGER, "Unknown geometry type: %d", static_cast<int>(geom->type));
98  break;
99  }
100 
101  return shapes::ShapePtr(result);
102 }
103 
104 Eigen::Isometry3d urdfPose2Eigen(const urdf::Pose& pose)
105 {
106  Eigen::Quaterniond q(pose.rotation.w, pose.rotation.x, pose.rotation.y, pose.rotation.z);
107  Eigen::Isometry3d result;
108  result.translation() = Eigen::Vector3d(pose.position.x, pose.position.y, pose.position.z);
109  result.linear() = q.toRotationMatrix();
110  return result;
111 }
112 
113 } // namespace collision_detection_bullet
Eigen::Isometry3d urdfPose2Eigen(const urdf::Pose &pose)
void getActiveLinkNamesRecursive(std::vector< std::string > &active_links, const urdf::LinkConstSharedPtr &urdf_link, bool active)
Recursively traverses robot from root to get all active links.
shapes::ShapePtr constructShape(const urdf::Geometry *geom)
Vec3fX< details::Vec3Data< double > > Vector3d
Definition: fcl_compat.h:89
const rclcpp::Logger BULLET_LOGGER