Tesseract
Motion Planning Environment
Loading...
Searching...
No Matches
collision_box_sphere_unit.hpp
Go to the documentation of this file.
1#ifndef TESSERACT_COLLISION_COLLISION_BOX_SPHERE_UNIT_HPP
2#define TESSERACT_COLLISION_COLLISION_BOX_SPHERE_UNIT_HPP
3
8
10{
11namespace detail
12{
13inline void addCollisionObjects(DiscreteContactManager& checker, bool use_convex_mesh = false)
14{
16 // Add box to checker
18 CollisionShapePtr box = std::make_shared<tesseract_geometry::Box>(1, 1, 1);
19 Eigen::Isometry3d box_pose;
20 box_pose.setIdentity();
21
22 CollisionShapesConst obj1_shapes;
24 obj1_shapes.push_back(box);
25 obj1_poses.push_back(box_pose);
26
27 checker.addCollisionObject("box_link", 0, obj1_shapes, obj1_poses, false);
28 checker.enableCollisionObject("box_link");
29
31 // Add thin box to checker which is disabled
33 CollisionShapePtr thin_box = std::make_shared<tesseract_geometry::Box>(0.1, 1, 1);
34 Eigen::Isometry3d thin_box_pose;
35 thin_box_pose.setIdentity();
36
37 CollisionShapesConst obj2_shapes;
39 obj2_shapes.push_back(thin_box);
40 obj2_poses.push_back(thin_box_pose);
41
42 checker.addCollisionObject("thin_box_link", 0, obj2_shapes, obj2_poses);
43 checker.disableCollisionObject("thin_box_link");
44
46 // Add sphere to checker. If use_convex_mesh = true then this
47 // sphere will be added as a convex hull mesh.
50
51 if (use_convex_mesh)
52 {
53 auto mesh_vertices = std::make_shared<tesseract_common::VectorVector3d>();
54 auto mesh_faces = std::make_shared<Eigen::VectorXi>();
56 std::string(TESSERACT_SUPPORT_DIR) + "/meshes/sphere_p25m.ply", *mesh_vertices, *mesh_faces, true),
57 0);
58
59 auto mesh = std::make_shared<tesseract_geometry::Mesh>(mesh_vertices, mesh_faces);
61 }
62 else
63 {
64 sphere = std::make_shared<tesseract_geometry::Sphere>(0.25);
65 }
66
67 Eigen::Isometry3d sphere_pose;
68 sphere_pose.setIdentity();
69
70 CollisionShapesConst obj3_shapes;
72 obj3_shapes.push_back(sphere);
73 obj3_poses.push_back(sphere_pose);
74
75 checker.addCollisionObject("sphere_link", 0, obj3_shapes, obj3_poses);
76
78 // Add box and remove
80 CollisionShapePtr remove_box = std::make_shared<tesseract_geometry::Box>(0.1, 1, 1);
81 Eigen::Isometry3d remove_box_pose;
82 thin_box_pose.setIdentity();
83
84 CollisionShapesConst obj4_shapes;
86 obj4_shapes.push_back(remove_box);
87 obj4_poses.push_back(remove_box_pose);
88
89 checker.addCollisionObject("remove_box_link", 0, obj4_shapes, obj4_poses);
90 EXPECT_TRUE(checker.getCollisionObjects().size() == 4);
91 EXPECT_TRUE(checker.hasCollisionObject("remove_box_link"));
92 checker.removeCollisionObject("remove_box_link");
93 EXPECT_FALSE(checker.hasCollisionObject("remove_box_link"));
94
96 // Try functions on a link that does not exist
98 EXPECT_FALSE(checker.removeCollisionObject("link_does_not_exist"));
99 EXPECT_FALSE(checker.enableCollisionObject("link_does_not_exist"));
100 EXPECT_FALSE(checker.disableCollisionObject("link_does_not_exist"));
101
103 // Try to add empty Collision Object
106 checker.addCollisionObject("empty_link", 0, CollisionShapesConst(), tesseract_common::VectorIsometry3d()));
107
109 // Check sizes
111 EXPECT_TRUE(checker.getCollisionObjects().size() == 3);
112 for (const auto& co : checker.getCollisionObjects())
113 {
114 EXPECT_TRUE(checker.getCollisionObjectGeometries(co).size() == 1);
115 EXPECT_TRUE(checker.getCollisionObjectGeometriesTransforms(co).size() == 1);
116 for (const auto& cgt : checker.getCollisionObjectGeometriesTransforms(co))
117 {
118 EXPECT_TRUE(cgt.isApprox(Eigen::Isometry3d::Identity(), 1e-5));
119 }
120 }
121}
122
124{
126 // Test when object is in collision
128 std::vector<std::string> active_links{ "box_link", "sphere_link" };
130 std::vector<std::string> check_active_links = checker.getActiveCollisionObjects();
132
133 EXPECT_TRUE(checker.getIsContactAllowedFn() == nullptr);
134
137
138 // Set the collision object transforms
140 location["box_link"] = Eigen::Isometry3d::Identity();
141 location["sphere_link"] = Eigen::Isometry3d::Identity();
142 location["sphere_link"].translation()(0) = 0.2;
143 checker.setCollisionObjectsTransform(location);
144
145 // Perform collision check
146 ContactResultMap result;
148
151
152 EXPECT_TRUE(!result_vector.empty());
153 EXPECT_NEAR(result_vector[0].distance, -0.55, 0.001);
154
155 std::vector<int> idx = { 0, 1, 1 };
156 if (result_vector[0].link_names[0] != "box_link")
157 idx = { 1, 0, -1 };
158
160 {
162 EXPECT_FALSE(std::abs(result_vector[0].nearest_points[0][0] - (0.5)) > 0.001 &&
163 std::abs(result_vector[0].nearest_points[0][0] - (-0.05)) > 0.001);
164 EXPECT_NEAR(result_vector[0].nearest_points[0][1], 0.0, 0.001);
165 EXPECT_NEAR(result_vector[0].nearest_points[0][2], 0.0, 0.001);
166 }
167 else
168 {
169 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][0], 0.5, 0.001);
170 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][1], 0.0, 0.001);
171 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][2], 0.0, 0.001);
172 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][0], -0.05, 0.001);
173 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][1], 0.0, 0.001);
174 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][2], 0.0, 0.001);
175 }
176
177 EXPECT_NEAR(result_vector[0].normal[0], idx[2] * 1.0, 0.001);
178 EXPECT_NEAR(result_vector[0].normal[1], idx[2] * 0.0, 0.001);
179 EXPECT_NEAR(result_vector[0].normal[2], idx[2] * 0.0, 0.001);
180
182 // Test object is out side the contact distance
184 location["sphere_link"].translation() = Eigen::Vector3d(1, 0, 0);
185 result.clear();
186 result_vector.clear();
187
188 // Use different method for setting transforms
189 checker.setCollisionObjectsTransform("sphere_link", location["sphere_link"]);
192
193 EXPECT_TRUE(result_vector.empty());
194
196 // Test object inside the contact distance
198 result.clear();
199 result_vector.clear();
200
205
206 EXPECT_TRUE(!result_vector.empty());
207 EXPECT_NEAR(result_vector[0].distance, 0.25, 0.001);
208
209 idx = { 0, 1, 1 };
210 if (result_vector[0].link_names[0] != "box_link")
211 idx = { 1, 0, -1 };
212
214 {
216 EXPECT_FALSE(std::abs(result_vector[0].nearest_points[0][0] - (0.5)) > 0.001 &&
217 std::abs(result_vector[0].nearest_points[0][0] - (0.75)) > 0.001);
218 EXPECT_NEAR(result_vector[0].nearest_points[0][1], 0.0, 0.001);
219 EXPECT_NEAR(result_vector[0].nearest_points[0][2], 0.0, 0.001);
220 }
221 else
222 {
223 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][0], 0.5, 0.001);
224 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][1], 0.0, 0.001);
225 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][2], 0.0, 0.001);
226 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][0], 0.75, 0.001);
227 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][1], 0.0, 0.001);
228 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][2], 0.0, 0.001);
229 }
230
231 EXPECT_NEAR(result_vector[0].normal[0], idx[2] * 1.0, 0.001);
232 EXPECT_NEAR(result_vector[0].normal[1], idx[2] * 0.0, 0.001);
233 EXPECT_NEAR(result_vector[0].normal[2], idx[2] * 0.0, 0.001);
234}
235
237{
239 // Test when object is in collision
241 std::vector<std::string> active_links{ "box_link", "sphere_link" };
243 std::vector<std::string> check_active_links = checker.getActiveCollisionObjects();
245
246 EXPECT_TRUE(checker.getIsContactAllowedFn() == nullptr);
247
250
251 // Set the collision object transforms
253 location["box_link"] = Eigen::Isometry3d::Identity();
254 location["sphere_link"] = Eigen::Isometry3d::Identity();
255 location["sphere_link"].translation()(0) = 0.2;
256 checker.setCollisionObjectsTransform(location);
257
258 // Perform collision check
259 ContactResultMap result;
261
264
265 EXPECT_TRUE(!result_vector.empty());
266 EXPECT_NEAR(result_vector[0].distance, -0.53776, 0.001);
269
270 std::vector<int> idx = { 0, 1, 1 };
271 if (result_vector[0].link_names[0] != "box_link")
272 idx = { 1, 0, -1 };
273
275 {
277 EXPECT_FALSE(std::abs(result_vector[0].nearest_points[0][0] - (0.5)) > 0.001 &&
278 std::abs(result_vector[0].nearest_points[0][0] - (-0.03776)) > 0.001);
279 }
280 else
281 {
282 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][0], 0.5, 0.001);
283 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][0], -0.03776, 0.001);
284 }
285
286 EXPECT_GT((idx[2] * result_vector[0].normal).dot(Eigen::Vector3d(1, 0, 0)), 0.0);
287 EXPECT_LT(std::abs(std::acos((idx[2] * result_vector[0].normal).dot(Eigen::Vector3d(1, 0, 0)))), 0.00001);
288
290 // Test object is out side the contact distance
292 location["sphere_link"].translation() = Eigen::Vector3d(1, 0, 0);
293 result.clear();
294 result_vector.clear();
295 checker.setCollisionObjectsTransform(location);
296
299
300 EXPECT_TRUE(result_vector.empty());
301
303 // Test object inside the contact distance
305 result.clear();
306 result_vector.clear();
307
312
313 EXPECT_TRUE(!result_vector.empty());
314 EXPECT_NEAR(result_vector[0].distance, 0.26224, 0.001);
317
318 idx = { 0, 1, 1 };
319 if (result_vector[0].link_names[0] != "box_link")
320 idx = { 1, 0, -1 };
321
323 {
325 EXPECT_FALSE(std::abs(result_vector[0].nearest_points[0][0] - (0.5)) > 0.001 &&
326 std::abs(result_vector[0].nearest_points[0][0] - (0.76224)) > 0.001);
327 }
328 else
329 {
330 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[0])][0], 0.5, 0.001);
331 EXPECT_NEAR(result_vector[0].nearest_points[static_cast<size_t>(idx[1])][0], 0.76224, 0.001);
332 }
333
334 EXPECT_GT((idx[2] * result_vector[0].normal).dot(Eigen::Vector3d(1, 0, 0)), 0.0);
335 EXPECT_LT(std::abs(std::acos((idx[2] * result_vector[0].normal).dot(Eigen::Vector3d(1, 0, 0)))), 0.00001);
336}
337} // namespace detail
338
339inline void runTest(DiscreteContactManager& checker, bool use_convex_mesh = false)
340{
341 // Add collision objects
342 detail::addCollisionObjects(checker, use_convex_mesh);
343
344 // Call it again to test adding same object
345 detail::addCollisionObjects(checker, use_convex_mesh);
346
347 if (use_convex_mesh)
348 detail::runTestConvex(checker);
349 else
351}
352
353} // namespace tesseract_collision::test_suite
354#endif // TESSERACT_COLLISION_COLLISION_BOX_SPHERE_UNIT_HPP
This structure hold contact results for link pairs.
Definition: types.h:154
void clear()
This is a consurvative clear.
Definition: types.cpp:183
void flattenMoveResults(ContactResultVector &v)
Definition: types.cpp:215
void flattenCopyResults(ContactResultVector &v) const
Definition: types.cpp:227
Definition: discrete_contact_manager.h:41
virtual void setActiveCollisionObjects(const std::vector< std::string > &names)=0
Set which collision objects can move.
virtual void contactTest(ContactResultMap &collisions, const ContactRequest &request)=0
Perform a contact test for all objects based.
virtual void setCollisionObjectsTransform(const std::string &name, const Eigen::Isometry3d &pose)=0
Set a single collision object's transforms.
virtual void setDefaultCollisionMarginData(double default_collision_margin)=0
Set the default collision margin.
virtual const std::vector< std::string > & getActiveCollisionObjects() const =0
Get which collision objects can move.
virtual const CollisionMarginData & getCollisionMarginData() const =0
Get the contact distance threshold.
virtual void setCollisionMarginData(CollisionMarginData collision_margin_data, CollisionMarginOverrideType override_type=CollisionMarginOverrideType::REPLACE)=0
Set the contact distance thresholds for which collision should be considered on a per pair basis.
virtual IsContactAllowedFn getIsContactAllowedFn() const =0
Get the active function for determining if two links are allowed to be in collision.
double getMaxCollisionMargin() const
Get the largest collision margin.
Definition: collision_margin_data.h:165
EXPECT_NEAR(config.contact_manager_config.margin_data.getDefaultCollisionMargin(), 5, 1e-6)
tesseract_collision::ContactResultVector result_vector
Definition: collision_core_unit.cpp:410
results distance
Definition: collision_core_unit.cpp:139
results link_names[0]
Definition: collision_core_unit.cpp:146
results normal
Definition: collision_core_unit.cpp:154
EXPECT_FALSE(tesseract_collision::isContactAllowed("base_link", "link_2", acm, false))
results nearest_points[0]
Definition: collision_core_unit.cpp:140
results single_contact_point
Definition: collision_core_unit.cpp:161
EXPECT_TRUE(tesseract_common::isIdentical< tesseract_collision::ObjectPairKey >(pairs, check_pairs, false))
This is a collection of common methods.
This is a collection of common methods.
This is the discrete contact manager base class.
Tesseract Geometries.
EXPECT_GT(m.m.condition, 1e+20)
void addCollisionObjects(ContinuousContactManager &checker)
Definition: collision_box_box_cast_unit.hpp:11
void runTestConvex(DiscreteContactManager &checker)
Definition: collision_box_sphere_unit.hpp:236
void runTestPrimitive(DiscreteContactManager &checker)
Definition: collision_box_sphere_unit.hpp:123
Definition: large_dataset_benchmarks.hpp:17
void runTest(ContinuousContactManager &checker)
Definition: collision_box_box_cast_unit.hpp:111
tesseract_common::CollisionMarginData CollisionMarginData
Definition: types.h:50
tesseract_geometry::Geometry::Ptr CollisionShapePtr
Definition: types.h:49
std::vector< tesseract_geometry::Geometry::ConstPtr > CollisionShapesConst
Definition: types.h:47
tesseract_common::AlignedVector< ContactResult > ContactResultVector
Definition: types.h:134
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:285
tesseract_geometry::ConvexMesh::Ptr makeConvexMesh(const tesseract_geometry::Mesh &mesh)
Definition: convex_hull_utils.cpp:114
AlignedMap< std::string, Eigen::Isometry3d > TransformMap
Definition: types.h:66
template bool isIdentical< std::string >(const std::vector< std::string > &, const std::vector< std::string > &, bool, const std::function< bool(const std::string &, const std::string &)> &, const std::function< bool(const std::string &, const std::string &)> &)
AlignedVector< Eigen::Isometry3d > VectorIsometry3d
Definition: types.h:62
The ContactRequest struct.
Definition: types.h:294
std::vector< std::string > active_links
Definition: tesseract_environment_collision.cpp:112
auto box
Definition: tesseract_geometry_unit.cpp:18
auto sphere
Definition: tesseract_geometry_unit.cpp:23
auto mesh
Definition: tesseract_geometry_unit.cpp:25