diff --git a/include/xbot2_interface/collision.h b/include/xbot2_interface/collision.h index aca762d..aa85d61 100644 --- a/include/xbot2_interface/collision.h +++ b/include/xbot2_interface/collision.h @@ -58,6 +58,12 @@ struct XBOT2IFC_API Shape std::shared_ptr height; }; + struct MeshRaw + { + std::vector vertices; + std::vector triangles; + }; + using Variant = std::variant< Sphere, Capsule, @@ -65,7 +71,8 @@ struct XBOT2IFC_API Shape Cylinder, Mesh, Octree, - HeightMap + HeightMap, + MeshRaw >; }; diff --git a/src/collision/collision.cpp b/src/collision/collision.cpp index 634f9bf..a87d0f1 100644 --- a/src/collision/collision.cpp +++ b/src/collision/collision.cpp @@ -550,6 +550,38 @@ bool CollisionModel::Impl::addCollisionShape(string_const_ref name, return false; }, + [&](const Shape::MeshRaw& mr) + { + // fill vertices and triangles + std::vector vertices; + std::vector triangles; + + for(unsigned int i = 0; i < mr.vertices.size(); ++i) + { + fcl::Vec3f v(mr.vertices[i].x(), + mr.vertices[i].y(), + mr.vertices[i].z()); + vertices.push_back(v); + } + + for(unsigned int i = 0; i < mr.triangles.size(); ++i) + { + fcl::Triangle t(mr.triangles[i][0], mr.triangles[i][1], mr.triangles[i][2]); + triangles.push_back(t); + } + + // add the mesh data into the BVHModel structure + auto bvhModel = std::make_shared>(); + fcl_geom = bvhModel; + bvhModel->beginModel(vertices.size(), triangles.size()); + bvhModel->addSubModel(vertices, triangles); + bvhModel->endModel(); + + std::cout << "mesh raw"; + + + return true; + }, [&](const Shape::Mesh& m) { // read mesh file @@ -1173,6 +1205,14 @@ void CollisionModel::Impl::CollisionPairData::compute_distance(const ModelInterf { dresult.normal = (dresult.nearest_points[1]-dresult.nearest_points[0]).normalized(); } + + //hack to fix normal direction when not pointing from o1 to o2 + double dir = dresult.normal.dot(dresult.nearest_points[1] - dresult.nearest_points[0]); + if(dir < 1e-6) + { + dresult.normal = -dresult.normal; + } + } void CollisionModel::Impl::CollisionPairData::compute_collision(const ModelInterface &model,