Skip to content

Commit 00a03ba

Browse files
Fix use of compound mesh in FCL
1 parent 5ad42b6 commit 00a03ba

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

tesseract_collision/bullet/src/bullet_utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,11 @@ CollisionObjectWrapper::CollisionObjectWrapper(std::string name,
472472
{
473473
if (m_shapes[j]->getType() == tesseract_geometry::GeometryType::COMPOUND_MESH)
474474
{
475+
int shape_index = shape_id++;
475476
const auto& meshes = std::static_pointer_cast<const tesseract_geometry::CompoundMesh>(m_shapes[j])->getMeshes();
476477
for (const auto& mesh : meshes)
477478
{
478-
std::shared_ptr<btCollisionShape> subshape = createShapePrimitive(mesh, this, shape_id++);
479+
std::shared_ptr<btCollisionShape> subshape = createShapePrimitive(mesh, this, shape_index);
479480
if (subshape != nullptr)
480481
{
481482
manage(subshape);

tesseract_collision/fcl/include/tesseract_collision/fcl/fcl_collision_object_wrapper.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,23 @@ class FCLCollisionObjectWrapper : public fcl::CollisionObject<double>
6464
*/
6565
void updateAABB();
6666

67+
/**
68+
* @brief Set the shape index. This is the geometries index in the urdf.
69+
* @param index The index
70+
*/
71+
void setShapeIndex(int index);
72+
73+
/**
74+
* @brief Get the shape index. This is the geometries index in the urdf.
75+
* @return The shape index
76+
*/
77+
int getShapeIndex() const;
78+
6779
protected:
6880
double contact_distance_{ 0 }; /**< @brief The contact distance threshold. */
81+
82+
/** @brief The shape index, which is the geometries index in the urdf. */
83+
int shape_index_{ -1 };
6984
};
7085

7186
} // namespace tesseract_collision::tesseract_collision_fcl

tesseract_collision/fcl/include/tesseract_collision/fcl/fcl_utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ class CollisionObjectWrapper
113113
void setCollisionObjectsTransform(const Eigen::Isometry3d& pose)
114114
{
115115
world_pose_ = pose;
116-
for (unsigned i = 0; i < collision_objects_.size(); ++i)
116+
for (auto& co : collision_objects_)
117117
{
118-
CollisionObjectPtr& co = collision_objects_[i];
119-
co->setTransform(pose * shape_poses_[i]);
118+
co->setTransform(pose * shape_poses_[static_cast<std::size_t>(co->getShapeIndex())]);
120119
co->updateAABB(); // This a tesseract function that updates abb to take into account contact distance
121120
}
122121
}
@@ -168,7 +167,7 @@ class CollisionObjectWrapper
168167
* @param co fcl collision shape
169168
* @return links collision shape index
170169
*/
171-
int getShapeIndex(const fcl::CollisionObjectd* co) const;
170+
static int getShapeIndex(const fcl::CollisionObjectd* co);
172171

173172
protected:
174173
std::string name_; // name of the collision object

tesseract_collision/fcl/src/fcl_collision_object_wrapper.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ void FCLCollisionObjectWrapper::updateAABB()
5454
}
5555
}
5656

57+
void FCLCollisionObjectWrapper::setShapeIndex(int index) { shape_index_ = index; }
58+
59+
int FCLCollisionObjectWrapper::getShapeIndex() const
60+
{
61+
assert(shape_index_ >= 0);
62+
return shape_index_;
63+
}
64+
5765
} // namespace tesseract_collision::tesseract_collision_fcl

tesseract_collision/fcl/src/fcl_utils.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ bool collisionCallback(fcl::CollisionObjectd* o1, fcl::CollisionObjectd* o2, voi
246246
ContactResult contact;
247247
contact.link_names[0] = cd1->getName();
248248
contact.link_names[1] = cd2->getName();
249-
contact.shape_id[0] = static_cast<int>(cd1->getShapeIndex(o1));
250-
contact.shape_id[1] = static_cast<int>(cd2->getShapeIndex(o2));
249+
contact.shape_id[0] = CollisionObjectWrapper::getShapeIndex(o1);
250+
contact.shape_id[1] = CollisionObjectWrapper::getShapeIndex(o2);
251251
contact.subshape_id[0] = static_cast<int>(fcl_contact.b1);
252252
contact.subshape_id[1] = static_cast<int>(fcl_contact.b2);
253253
contact.nearest_points[0] = fcl_contact.pos;
@@ -307,8 +307,8 @@ bool distanceCallback(fcl::CollisionObjectd* o1, fcl::CollisionObjectd* o2, void
307307
ContactResult contact;
308308
contact.link_names[0] = cd1->getName();
309309
contact.link_names[1] = cd2->getName();
310-
contact.shape_id[0] = cd1->getShapeIndex(o1);
311-
contact.shape_id[1] = cd2->getShapeIndex(o2);
310+
contact.shape_id[0] = CollisionObjectWrapper::getShapeIndex(o1);
311+
contact.shape_id[1] = CollisionObjectWrapper::getShapeIndex(o2);
312312
contact.subshape_id[0] = static_cast<int>(fcl_result.b1);
313313
contact.subshape_id[1] = static_cast<int>(fcl_result.b2);
314314
contact.nearest_points[0] = fcl_result.nearest_points[0];
@@ -365,6 +365,7 @@ CollisionObjectWrapper::CollisionObjectWrapper(std::string name,
365365
collision_geometries_.push_back(subshape);
366366
auto co = std::make_shared<FCLCollisionObjectWrapper>(subshape);
367367
co->setUserData(this);
368+
co->setShapeIndex(static_cast<int>(i));
368369
co->setTransform(shape_poses_[i]);
369370
co->updateAABB();
370371
collision_objects_.push_back(co);
@@ -380,6 +381,7 @@ CollisionObjectWrapper::CollisionObjectWrapper(std::string name,
380381
collision_geometries_.push_back(subshape);
381382
auto co = std::make_shared<FCLCollisionObjectWrapper>(subshape);
382383
co->setUserData(this);
384+
co->setShapeIndex(static_cast<int>(i));
383385
co->setTransform(shape_poses_[i]);
384386
co->updateAABB();
385387
collision_objects_.push_back(co);
@@ -389,16 +391,9 @@ CollisionObjectWrapper::CollisionObjectWrapper(std::string name,
389391
}
390392
}
391393

392-
int CollisionObjectWrapper::getShapeIndex(const fcl::CollisionObjectd* co) const
394+
int CollisionObjectWrapper::getShapeIndex(const fcl::CollisionObjectd* co)
393395
{
394-
auto it = std::find_if(collision_objects_.begin(), collision_objects_.end(), [&co](const CollisionObjectPtr& c) {
395-
return c.get() == co;
396-
});
397-
398-
if (it != collision_objects_.end())
399-
return static_cast<int>(std::distance(collision_objects_.begin(), it));
400-
401-
return -1;
396+
return static_cast<const FCLCollisionObjectWrapper*>(co)->getShapeIndex();
402397
}
403398

404399
} // namespace tesseract_collision::tesseract_collision_fcl

tesseract_collision/test_suite/include/tesseract_collision/test_suite/collision_compound_mesh_sphere_unit.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ inline void runTest(DiscreteContactManager& checker)
177177
EXPECT_NEAR(result_vector[0].normal[2], idx[2] * -1.0, 0.001);
178178

179179
// Compound mesh so check shape id
180-
EXPECT_EQ(result_vector[0].shape_id[static_cast<size_t>(idx[0])], 1);
180+
EXPECT_EQ(result_vector[0].shape_id[static_cast<size_t>(idx[0])], 0);
181181

182182
////////////////////////////////////////////////
183183
// Test object is out side the contact distance
@@ -235,7 +235,7 @@ inline void runTest(DiscreteContactManager& checker)
235235
EXPECT_NEAR(result_vector[0].normal[2], idx[2] * -1.0, 0.001);
236236

237237
// Compound mesh so check shape id
238-
EXPECT_EQ(result_vector[0].shape_id[static_cast<size_t>(idx[0])], 1);
238+
EXPECT_EQ(result_vector[0].shape_id[static_cast<size_t>(idx[0])], 0);
239239

240240
/////////////////////////////////////////////
241241
// Test collision against second shape

0 commit comments

Comments
 (0)