windows下使用FCL(Flexible-collision-library)

2023-11-06 19:36

本文主要是介绍windows下使用FCL(Flexible-collision-library),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

windows下使用FCL(The Flexible-collision-library)

   FCL做为一款开源的碰撞检测库,支持多种基础的几何体,及支持C++和python,在windows和linux平台均可以使用。是一款计算高效的碰撞检测工具。在机械臂规划控制框架moveit中做为基础的碰撞检测算法。
FCL支持的几何体类型:

  • box (长方体)
  • sphere(球)
  • ellipsoid(椭球)
  • capsule(胶囊体)
  • cone(锥体)
  • cylinder(圆柱)
  • convex(凸包)
  • half-space(半空间)
  • plane(平面)
  • mesh(面片)
  • octree (八叉树)

FCL库(The Flexible Collision Library)主要的功能有:
1、碰撞检测:检测两个模型是否重叠,以及(可选)所有重叠的三角形。
2、距离计算:计算一对模型之间的最小距离,即最近的一对点之间的距离。
3、公差验证:确定两个模型是否比公差距离更近或更远。
4、连续碰撞检测:检测两个运动模型在运动过程中是否重叠,以及可选的接触时间。
5、接触信息:对于碰撞检测和连续碰撞检测,可以选择返回接触信息(包括接触法线和接触点)。

源码下载及编译

FCL 源码github
  在windows环境下,使用VS studio直接编译FCL存在问题,需要将CMake设置成Release版本以及屏蔽掉测试程序。具体操作如下:

  1. 使用VS studio打开FCO源码工程,如图1所示。

图1
2. 通过改CMakeList.txt文件,屏蔽测试程序,如图2所示。

图2
3. 点击“项目”,再点击“fcl的CMake配置”,将编译设置成Release版本,如图3所示。

图3
4. 点击“生成”,再点击“全部重新生成”对FCL源码进行编译。

FCL碰撞测试demo

  测试程序如下所示:

//main.cpp
#include "fcl/math/constants.h"
#include "fcl/narrowphase/collision.h"
#include "fcl/narrowphase/collision_object.h"
#include "fcl/narrowphase/distance.h"/*** @brief 两个相互碰撞的Box碰撞检测测试*/
void test1() {std::shared_ptr<fcl::CollisionGeometry<double>> box1(new fcl::Box<double>(3, 3, 3));std::shared_ptr<fcl::CollisionGeometry<double>> box2(new fcl::Box<double>(1, 1, 1));fcl::Transform3d tf1 = fcl::Transform3d::Identity();fcl::CollisionObjectd obj1(box1, tf1);fcl::Transform3d tf2 = fcl::Transform3d::Identity();fcl::CollisionObjectd obj2(box2, tf2);fcl::CollisionRequestd request;fcl::CollisionResultd result;request.gjk_solver_type =fcl::GJKSolverType::GST_INDEP;  // specify solver type with the default// type is GST_LIBCCDfcl::collide(&obj1, &obj2, request, result);std::cout << "test1 collide result:" << result.isCollision() << std::endl;
}/*** @brief 两个无碰撞的Box碰撞检测测试*/
void test2() {std::shared_ptr<fcl::CollisionGeometry<double>> box1(new fcl::Box<double>(3, 3, 3));std::shared_ptr<fcl::CollisionGeometry<double>> box2(new fcl::Box<double>(1, 1, 1));fcl::Transform3d tf1 = fcl::Transform3d::Identity();fcl::CollisionObjectd obj1(box1, tf1);fcl::Transform3d tf2 = fcl::Transform3d::Identity();tf2.translation() = fcl::Vector3d{3, 0, 0};fcl::CollisionObjectd obj2(box2, tf2);fcl::CollisionRequestd request;fcl::CollisionResultd result;fcl::collide(&obj1, &obj2, request, result);std::cout << "test2 collide result:" << result.isCollision() << std::endl;
}/*** @brief 两个无碰撞的Box碰撞检测测试,并计算最短距离*/
void test3() {std::shared_ptr<fcl::CollisionGeometry<double>> box1(new fcl::Box<double>(3, 3, 3));std::shared_ptr<fcl::CollisionGeometry<double>> box2(new fcl::Box<double>(1, 1, 1));fcl::Transform3d tf1 = fcl::Transform3d::Identity();fcl::CollisionObjectd obj1(box1, tf1);fcl::Transform3d tf2 = fcl::Transform3d::Identity();tf2.translation() = fcl::Vector3d{3, 0, 0};fcl::CollisionObjectd obj2(box2, tf2);fcl::CollisionRequestd request;fcl::CollisionResultd result;// fcl::collide(&obj1,&obj2,request,result);std::cout << "test3 collide result:" << result.isCollision() << std::endl;fcl::DistanceRequestd dist_request(true);dist_request.distance_tolerance = 1e-4;fcl::DistanceResultd dist_result;fcl::distance(&obj1, &obj2, dist_request, dist_result);std::cout << "test3 collide distance:" << dist_result.min_distance<< std::endl;std::cout << "test3 collide point 0:" << dist_result.nearest_points[0]<< std::endl;std::cout << "test3 collide point 1:" << dist_result.nearest_points[1]<< std::endl;
}/*** @brief 加载STL模型*/
bool loadSTLFile(const std::string& filename,std::vector<fcl::Triangle>& triangles) {std::ifstream file(filename, std::ios::in | std::ios::binary);if (!file) {std::cerr << "Failed to open STL file: " << filename << std::endl;return false;}file.seekg(0, std::ios::end);  /// 定位到流末尾的位置,0偏移std::streampos length = file.tellg();  /// 记录当前指针位置file.seekg(0, std::ios::beg);  /// 定位到流开头的位置,0偏移std::vector<char> buffer(length);file.read(&buffer[0], length);file.close();if (length < 84) {std::cerr << "Invalid STL file: " << filename << std::endl;return false;}unsigned int num_triangles = *(unsigned int*)&buffer[80];triangles.resize(num_triangles);unsigned int offset = 84;for (unsigned int i = 0; i < num_triangles; ++i) {for (unsigned int j = 0; j < 3; ++j) {// 3顶点构成三角形float* vertex = (float*)&buffer[offset + j * 12];triangles[i][j] = (vertex[0], vertex[1], vertex[2]);}offset += 50;}return true;
}/*** @brief 在STL文件格式中,文件头部分包含80个字节的文件头信息和4个字节的三角形数量信息,因此文件总长度至少为84个字节。
因此,在loadSTLFile函数中我们首先检查文件长度是否小于84个字节,如果是则认为文件格式非法。
在STL文件中,每个三角形由12个浮点数和2个无用字节组成,因此每个三角形占用50个字节。
因此,在loadSTLFile函数中,我们通过一个循环遍历每个三角形,并从文件中读取对应的12个浮点数,最后将三角形的3个顶点存储在一个fcl::Triangle类型的变量中。
每次读取完一个三角形后,需要将读取指针向前移动50个字节,即offset += 50。由于文件头部分占用了前84个字节,因此,在开始循环前需要将读取指针初始化为offset= 84,从而跳过文件头部分,开始读取三角形信息。*/
void test4() {std::vector<fcl::Triangle> triangles;  /// 创建三角片序列/// 加载模型if (!loadSTLFile("C:/test0.STL", triangles)) {std::cout << "Error:loadSTLFile failed!" << std::endl;return;}/// 创建mesh,并添加三角片到mesh///std::shared_ptr<fcl::BVHModel<fcl::OBBRSSd>> mesh_geometry(new fcl::BVHModel<fcl::OBBRSSd>());mesh_geometry->beginModel();for (const auto& triangle : triangles) {Eigen::Vector3d p1(triangle[0]), p2(triangle[1]), p3(triangle[2]);mesh_geometry->addTriangle(p1, p2, p3);}mesh_geometry->endModel();/// 建立碰撞对象-stl ,并添加CollisionGeometry,坐标位置(0,0,0)fcl::CollisionObjectd obj(mesh_geometry);/// 建立碰撞对象-box ,坐标位置(0,0,20)std::shared_ptr<fcl::Boxd> box1 = std::make_shared<fcl::Boxd>(2.0, 2.0, 2.0);fcl::CollisionObjectd obj1(box1);obj1.setTranslation(Eigen::Vector3d(0, 0, 0));fcl::CollisionRequestd request;fcl::CollisionResultd result;/// 进行碰撞检测fcl::collide(&obj, &obj1, request, result);/// 输出碰撞结果if (result.isCollision()) {std::cout << "Collision detected!" << std::endl;} else {std::cout << "No collision detected." << std::endl;}/// 距离检测fcl::DistanceRequestd requestd;fcl::DistanceResultd resultd;fcl::distance(&obj, &obj1, requestd, resultd);std::cout << "min_distance:" << resultd.min_distance << std::endl;
}int main(int argc, char** argv) {std::cout << "FCL test" << std::endl;test1();test2();test3();test4();std::cout << "end test" << std::endl;return 0;
}

CMakeList.txt文件如下所示:

cmake_minimum_required(VERSION 3.14)
find_package(Eigen3 REQUIRED)
find_package(FCL REQUIRED)
add_executable(use_fcl main.cpp)
target_link_libraries(use_fcl fcl Eigen3::Eigen)
target_include_directories(use_fcl PUBLIC ${EIGEN3_INCLUDE_DIRS} ${FCL_INCLUDE_DIRS})

这篇关于windows下使用FCL(Flexible-collision-library)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/358635

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解