collision_profile.cpp
Go to the documentation of this file.
3 #include <random>
4 #include <chrono>
5 #include <memory>
7 
14 
17 static const std::size_t DIM = 10;
18 
19 using namespace tesseract_collision;
20 using namespace tesseract_geometry;
21 
22 void addCollisionObjects(DiscreteContactManager& checker, bool use_single_link, bool use_convex_mesh)
23 {
25  // Add sphere to checker. If use_convex_mesh = true then this
26  // sphere will be added as a convex hull mesh.
28  CollisionShapePtr sphere;
29 
30  if (use_convex_mesh)
31  {
32  auto mesh_vertices = std::make_shared<tesseract_common::VectorVector3d>();
33  auto mesh_faces = std::make_shared<Eigen::VectorXi>();
34 
36  loadSimplePlyFile(locator.locateResource("package://tesseract_support/meshes/sphere_p25m.ply")->getFilePath(),
37  *mesh_vertices,
38  *mesh_faces,
39  true);
40 
41  auto mesh = std::make_shared<tesseract_geometry::Mesh>(mesh_vertices, mesh_faces);
42  sphere = makeConvexMesh(*mesh);
43  }
44  else
45  {
46  sphere = std::make_shared<Sphere>(0.25);
47  }
48 
49  Eigen::Isometry3d sphere_pose;
50  sphere_pose.setIdentity();
51 
52  CollisionShapesConst obj3_shapes;
54  obj3_shapes.push_back(sphere);
55  obj3_poses.push_back(sphere_pose);
56  checker.addCollisionObject("move_link", 0, obj3_shapes, obj3_poses);
57 
58  CollisionShapesConst link_shapes;
60  for (std::size_t x = 0; x < DIM; ++x)
61  {
62  for (std::size_t y = 0; y < DIM; ++y)
63  {
64  for (std::size_t z = 0; z < DIM; ++z)
65  {
66  Eigen::Isometry3d sphere_pose;
67  sphere_pose.setIdentity();
68  sphere_pose.translation() = Eigen::Vector3d(double(x), double(y), double(z));
69 
70  if (use_single_link)
71  {
72  link_shapes.push_back(sphere);
73  link_poses.push_back(sphere_pose);
74  }
75  else
76  {
77  CollisionShapesConst shapes;
79  shapes.push_back(sphere);
80  poses.push_back(sphere_pose);
81  std::string link_name = "sphere_link_" + std::to_string(x) + std::to_string(y) + std::to_string(z);
82  checker.addCollisionObject(link_name, 0, shapes, poses);
83  }
84  }
85  }
86  }
87  if (use_single_link)
88  checker.addCollisionObject("sphere_link", 0, link_shapes, link_poses);
89 
90  checker.setActiveCollisionObjects({ "move_link" });
91 }
92 
93 void addCollisionObjects(ContinuousContactManager& checker, bool use_single_link, bool use_convex_mesh)
94 {
96  // Add sphere to checker. If use_convex_mesh = true then this
97  // sphere will be added as a convex hull mesh.
99  CollisionShapePtr sphere;
100 
101  if (use_convex_mesh)
102  {
103  auto mesh_vertices = std::make_shared<tesseract_common::VectorVector3d>();
104  auto mesh_faces = std::make_shared<Eigen::VectorXi>();
105 
107  loadSimplePlyFile(locator.locateResource("package://tesseract_support/meshes/sphere_p25m.ply")->getFilePath(),
108  *mesh_vertices,
109  *mesh_faces,
110  true);
111 
112  auto mesh = std::make_shared<tesseract_geometry::Mesh>(mesh_vertices, mesh_faces);
113  sphere = makeConvexMesh(*mesh);
114  }
115  else
116  {
117  sphere = std::make_shared<Sphere>(0.25);
118  }
119 
120  Eigen::Isometry3d sphere_pose;
121  sphere_pose.setIdentity();
122 
123  CollisionShapesConst obj3_shapes;
125  obj3_shapes.push_back(sphere);
126  obj3_poses.push_back(sphere_pose);
127  checker.addCollisionObject("move_link", 0, obj3_shapes, obj3_poses);
128 
129  CollisionShapesConst link_shapes;
131  for (std::size_t x = 0; x < DIM; ++x)
132  {
133  for (std::size_t y = 0; y < DIM; ++y)
134  {
135  for (std::size_t z = 0; z < DIM; ++z)
136  {
137  Eigen::Isometry3d sphere_pose;
138  sphere_pose.setIdentity();
139  sphere_pose.translation() = Eigen::Vector3d(double(x), double(y), double(z));
140 
141  if (use_single_link)
142  {
143  link_shapes.push_back(sphere);
144  link_poses.push_back(sphere_pose);
145  }
146  else
147  {
148  CollisionShapesConst shapes;
150  shapes.push_back(sphere);
151  poses.push_back(sphere_pose);
152  std::string link_name = "sphere_link_" + std::to_string(x) + std::to_string(y) + std::to_string(z);
153  checker.addCollisionObject(link_name, 0, shapes, poses);
154  }
155  }
156  }
157  }
158  if (use_single_link)
159  checker.addCollisionObject("sphere_link", 0, link_shapes, link_poses);
160 
161  checker.setActiveCollisionObjects({ "move_link" });
162 }
163 
164 std::vector<Eigen::Isometry3d> getTransforms(std::size_t num_poses)
165 {
166  std::vector<Eigen::Isometry3d> poses(num_poses);
167  for (std::size_t i = 0; i < num_poses; ++i)
168  {
169  double x = (double(rand()) / RAND_MAX) * double(DIM);
170  double y = (double(rand()) / RAND_MAX) * double(DIM);
171  double z = (double(rand()) / RAND_MAX) * double(DIM);
172  poses[i] = Eigen::Isometry3d::Identity();
173  poses[i].translation() = Eigen::Vector3d(x, y, z);
174  }
175  return poses;
176 }
177 
178 void runDiscreteProfile(bool use_single_link, bool use_convex_mesh, double contact_distance)
179 {
180  auto bt_simple_checker = std::make_shared<tesseract_collision_bullet::BulletDiscreteSimpleManager>();
181  auto bt_bvh_checker = std::make_shared<tesseract_collision_bullet::BulletDiscreteBVHManager>();
182  auto fcl_bvh_checker = std::make_shared<tesseract_collision_fcl::FCLDiscreteBVHManager>();
183 
184  std::vector<Eigen::Isometry3d> poses = getTransforms(50);
185  std::vector<DiscreteContactManager::Ptr> checkers = { bt_simple_checker, bt_bvh_checker, fcl_bvh_checker };
186  std::vector<std::string> checker_names = { "BtSimple", "BtBVH", "FCLBVH" };
187  std::vector<long> checker_contacts = { 0, 0, 0 };
188 
189  std::printf("Total number of shape: %d\n", int(DIM * DIM * DIM));
190  // for (std::size_t i = 0; i < checkers.size(); ++i)
191  ContactResultMap result;
192  for (std::size_t i = 0; i < 2; ++i)
193  {
194  addCollisionObjects(*checkers[i], use_single_link, use_convex_mesh);
195  checkers[i]->setCollisionMarginData(CollisionMarginData(contact_distance));
196 
197  auto start_time = std::chrono::high_resolution_clock::now();
198  for (auto& pose : poses)
199  {
200  checkers[i]->setCollisionObjectsTransform("move_link", pose);
201 
202  // Perform collision check
203  result.clear();
204  checkers[i]->contactTest(result, ContactTestType::FIRST);
205  checker_contacts[i] += result.count();
206  }
207  auto end_time = std::chrono::high_resolution_clock::now();
208  double total_time = std::chrono::duration<double, std::milli>(end_time - start_time).count();
209 
210  std::printf(
211  "%15s | DT: %15.5f ms | Contacts: %d\n", checker_names[i].c_str(), total_time, int(checker_contacts[i]));
212  }
213 }
214 
215 void runContinuousProfile(bool use_single_link, bool use_convex_mesh, double contact_distance)
216 {
217  auto bt_simple_checker = std::make_shared<tesseract_collision_bullet::BulletCastSimpleManager>();
218  auto bt_bvh_checker = std::make_shared<tesseract_collision_bullet::BulletCastBVHManager>();
219 
220  std::vector<Eigen::Isometry3d> poses = getTransforms(50);
221  std::vector<ContinuousContactManager::Ptr> checkers = { bt_simple_checker, bt_bvh_checker };
222  std::vector<std::string> checker_names = { "BtCastSimple", "BtCastBVH" };
223  std::vector<long> checker_contacts = { 0, 0, 0 };
224 
225  Eigen::Isometry3d delta_pose;
226  delta_pose.setIdentity();
227  delta_pose.translation() = Eigen::Vector3d(0.5, 0.5, 0.5);
228 
229  std::printf("Total number of shape: %d\n", int(DIM * DIM * DIM));
230  // for (std::size_t i = 0; i < checkers.size(); ++i)
231  ContactResultMap result;
232  for (std::size_t i = 0; i < 2; ++i)
233  {
234  addCollisionObjects(*checkers[i], use_single_link, use_convex_mesh);
235  checkers[i]->setCollisionMarginData(CollisionMarginData(contact_distance));
236 
237  auto start_time = std::chrono::high_resolution_clock::now();
238  for (auto& pose : poses)
239  {
240  checkers[i]->setCollisionObjectsTransform("move_link", pose, pose * delta_pose);
241 
242  // Perform collision check
243  result.clear();
244  checkers[i]->contactTest(result, ContactTestType::FIRST);
245  checker_contacts[i] += result.count();
246  }
247  auto end_time = std::chrono::high_resolution_clock::now();
248  double total_time = std::chrono::duration<double, std::milli>(end_time - start_time).count();
249 
250  std::printf(
251  "%15s | DT: %15.5f ms | Contacts: %d\n", checker_names[i].c_str(), total_time, int(checker_contacts[i]));
252  }
253 }
254 
255 int main(int /*argc*/, char** /*argv*/)
256 {
257  std::printf("Discrete: One Primitive Shape per link\n");
258  runDiscreteProfile(false, false, 0.0);
259 
260  std::printf("Discrete: All Primitive Shape in single link\n");
261  runDiscreteProfile(true, false, 0.0);
262 
263  std::printf("Discrete: One Convex Shape per link\n");
264  runDiscreteProfile(false, true, 0.0);
265 
266  std::printf("Discrete: All Convex Shape in single link\n");
267  runDiscreteProfile(true, true, 0.0);
268 
269  std::printf("Continuous: One Primitive Shape per link\n");
270  runContinuousProfile(false, false, 0.0);
271 
272  std::printf("Continuous: All Primitive Shape in single link\n");
273  runContinuousProfile(true, false, 0.0);
274 
275  std::printf("Continuous: One Convex Shape per link\n");
276  runContinuousProfile(false, true, 0.0);
277 
278  std::printf("Continuous: All Convex Shape in single link\n");
279  runContinuousProfile(true, true, 0.0);
280  return 0;
281 }
tesseract_common::VectorIsometry3d
AlignedVector< Eigen::Isometry3d > VectorIsometry3d
addCollisionObjects
void addCollisionObjects(DiscreteContactManager &checker, bool use_single_link, bool use_convex_mesh)
Definition: collision_profile.cpp:22
DIM
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH static const TESSERACT_COMMON_IGNORE_WARNINGS_POP std::size_t DIM
Definition: collision_profile.cpp:17
bullet_cast_simple_manager.h
Tesseract ROS Bullet cast(continuous) simple collision manager.
fcl_discrete_managers.h
Tesseract ROS FCL contact checker implementation.
bullet_cast_bvh_manager.h
Tesseract ROS Bullet cast(continuous) BVH collision manager.
bullet_discrete_simple_manager.h
Tesseract ROS Bullet discrete simple collision manager.
resource_locator.h
getTransforms
std::vector< Eigen::Isometry3d > getTransforms(std::size_t num_poses)
Definition: collision_profile.cpp:164
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_collision::ContactResultMap::clear
void clear()
This is a consurvative clear.
Definition: types.cpp:231
tesseract_collision::DiscreteContactManager
Definition: discrete_contact_manager.h:41
tesseract_collision::loadSimplePlyFile
int loadSimplePlyFile(const std::string &path, tesseract_common::VectorVector3d &vertices, Eigen::VectorXi &faces, bool triangles_only=false)
Loads a simple ply file given a path.
Definition: common.cpp:289
convex_hull_utils.h
This is a collection of common methods.
tesseract_collision::makeConvexMesh
tesseract_geometry::ConvexMesh::Ptr makeConvexMesh(const tesseract_geometry::Mesh &mesh)
Definition: convex_hull_utils.cpp:113
tesseract_collision::ContinuousContactManager
Definition: continuous_contact_manager.h:41
tesseract_collision::ContactResultMap
This structure hold contact results for link pairs.
Definition: types.h:155
tesseract_collision::DiscreteContactManager::addCollisionObject
virtual bool addCollisionObject(const std::string &name, const int &mask_id, const CollisionShapesConst &shapes, const tesseract_common::VectorIsometry3d &shape_poses, bool enabled=true)=0
Add a object to the checker.
tesseract_collision::ContinuousContactManager::addCollisionObject
virtual bool addCollisionObject(const std::string &name, const int &mask_id, const CollisionShapesConst &shapes, const tesseract_common::VectorIsometry3d &shape_poses, bool enabled=true)=0
Add a collision object to the checker.
tesseract_collision::CollisionShapePtr
std::shared_ptr< tesseract_geometry::Geometry > CollisionShapePtr
Definition: types.h:50
tesseract_collision::CollisionShapesConst
std::vector< CollisionShapeConstPtr > CollisionShapesConst
Definition: types.h:51
bullet_discrete_bvh_manager.h
Tesseract ROS Bullet discrete BVH collision manager.
sphere.h
runContinuousProfile
void runContinuousProfile(bool use_single_link, bool use_convex_mesh, double contact_distance)
Definition: collision_profile.cpp:215
TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: create_convex_hull.cpp:37
tesseract_common::GeneralResourceLocator::locateResource
std::shared_ptr< Resource > locateResource(const std::string &url) const override
tesseract_collision::ContactTestType::FIRST
@ FIRST
tesseract_collision::DiscreteContactManager::setActiveCollisionObjects
virtual void setActiveCollisionObjects(const std::vector< std::string > &names)=0
Set which collision objects can move.
tesseract_collision::ContactResultMap::count
long count() const
Get the total number of contact results storted.
Definition: types.cpp:212
tesseract_geometry
tesseract_common::GeneralResourceLocator
tesseract_collision
Definition: bullet_cast_bvh_manager.h:48
macros.h
main
int main(int, char **)
Definition: collision_profile.cpp:255
tesseract_collision::ContinuousContactManager::setActiveCollisionObjects
virtual void setActiveCollisionObjects(const std::vector< std::string > &names)=0
Set which collision objects can move.
tesseract_collision::CollisionMarginData
tesseract_common::CollisionMarginData CollisionMarginData
Definition: types.h:52
runDiscreteProfile
void runDiscreteProfile(bool use_single_link, bool use_convex_mesh, double contact_distance)
Definition: collision_profile.cpp:178


tesseract_collision
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:52