Skip to content

Commit

Permalink
passes all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexseal committed Mar 28, 2024
1 parent 270c4c4 commit 4db4caa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pybind/collision_detection/fcl/pybind_fcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <pybind11/stl.h>

#include "docstring/collision_detection/fcl/fcl.h"
#include "mplib/collision_detection/fcl/types.h"
#include "mplib/types.h"
#include "mplib/utils/conversion.h"
#include "pybind_macros.hpp"
Expand Down Expand Up @@ -41,6 +42,7 @@ using DistanceResult = fcl::DistanceResult<S>;
using Contact = fcl::Contact<S>;
using ContactPoint = fcl::ContactPoint<S>;
using CostSource = fcl::CostSource<S>;
using FCLObject = fcl::FCLObject<S>;

void build_pyfcl(py::module &m) {
// Data type
Expand Down Expand Up @@ -328,6 +330,12 @@ void build_pyfcl(py::module &m) {
.def_readonly("cost_density", &CostSource::cost_density)
.def_readonly("total_cost", &CostSource::total_cost);

// FCLObject
auto PyFCLObject = py::class_<FCLObject, std::shared_ptr<FCLObject>>(m, "FCLObject");
PyFCLObject.def(py::init<>())
.def_readwrite("collision_objects", &FCLObject::collision_objects_)
.def_readwrite("tfs", &FCLObject::tfs_);

// collide / distance functions
m.def("collide",
[](const CollisionObject *o1, const CollisionObject *o2,
Expand Down
5 changes: 2 additions & 3 deletions src/collision_detection/fcl/fcl_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void FCLModelTpl<S>::init(const urdf::ModelInterfaceSharedPtr &urdf_model,
template <typename S>
void FCLModelTpl<S>::dfsParseTree(const urdf::LinkConstSharedPtr &link,
const std::string &parent_link_name) {
if (link->collision)
if (link->collision) {
for (const auto &geom : link->collision_array) {
auto geom_model = geom->geometry;
fcl::CollisionGeometryPtr<S> collision_geometry;
Expand Down Expand Up @@ -121,6 +121,7 @@ void FCLModelTpl<S>::dfsParseTree(const urdf::LinkConstSharedPtr &link,
collision_link_names_.push_back(link->name);
parent_link_names_.push_back(parent_link_name);
}
}
for (const auto &child : link->child_links) dfsParseTree(child, link->name);
}

Expand Down Expand Up @@ -182,8 +183,6 @@ void FCLModelTpl<S>::removeCollisionPairsFromSRDF(const std::string &srdf_filena
template <typename S>
void FCLModelTpl<S>::updateCollisionObjects(
const std::vector<Isometry3<S>> &link_pose) const {
ASSERT(link_pose.size() == collision_objects_.size(),
"The size of link poses does not match the size of collision objects.");
for (size_t i = 0; i < collision_objects_.size(); i++) {
auto link_i = collision_link_user_indices_[i];
auto link_wrt_world = link_pose[link_i];
Expand Down
6 changes: 4 additions & 2 deletions src/collision_detection/fcl/fcl_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ void collideFCLObjects(const fcl::FCLObject<S> &o1, const fcl::FCLObject<S> &o2,
for (const auto &co_obj2 : o2.collision_objects_) {
fcl::CollisionResult<S> partial_result;
auto cost_sources = std::vector<fcl::CostSource<S>>();
partial_result.getCostSources(cost_sources);
fcl::collide(co_obj1.get(), co_obj2.get(), request, partial_result);
for (size_t i = 0; i < partial_result.numContacts(); i++) {
result.addContact(partial_result.getContact(i));
result.addCostSource(cost_sources[i], request.num_max_cost_sources);
}
partial_result.getCostSources(cost_sources);
for (auto &cost_source : cost_sources) {
result.addCostSource(cost_source, request.num_max_cost_sources);
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions tests/test_fcl_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,26 @@ def setUp(self):
for i, link_name in enumerate(self.collision_link_names):
link_idx = self.pinocchio_model.get_link_names().index(link_name)
link_pose = self.pinocchio_model.get_link_pose(link_idx)
self.model.get_collision_objects()[i].set_transformation(link_pose)
self.model.get_collision_objects()[i].collision_objects[
0
].set_transformation(link_pose)

def test_get_collision_objects(self):
self.assertEqual(
self.model.get_collision_link_names(), self.collision_link_names
)
for i, link_name in enumerate(self.collision_link_names):
pinocchio_idx = self.pinocchio_model.get_link_names().index(link_name)
pos = self.model.get_collision_objects()[i].get_translation()
quat = mat2quat(self.model.get_collision_objects()[i].get_rotation())
pos = (
self.model.get_collision_objects()[i]
.collision_objects[0]
.get_translation()
)
quat = mat2quat(
self.model.get_collision_objects()[i]
.collision_objects[0]
.get_rotation()
)
pinocchio_pose = self.pinocchio_model.get_link_pose(pinocchio_idx)
self.assertTrue(np.allclose(pos, pinocchio_pose[:3]))
self.assertAlmostEqual(abs(quat.dot(pinocchio_pose[3:])), 1)
Expand Down

0 comments on commit 4db4caa

Please sign in to comment.