版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15642411/article/details/87884871
CGAL是使用C++开发的计算几何算法库,提供Delaunay三角网,网格生成,多边形,以及各种几何处理算法。应用领域:计算机图形学,科学可视化,计算机辅助设计与建模,地理信息系统,分子生物学,医学影像学,机器人学和运动规划,和数值方法。
1、安装
$ sudo apt-get install libcgal-dev # CGAL库
$ sudo apt-get install libcgal-demo # CGAL例子
解压demo
$ mkdir $HOME/cgal
$ cd $HOME/cgal
$ tar xzf /usr/share/doc/libcgal-demo/examples.tar.gz
$ tar xzf /usr/share/doc/libcgal-demo/demo.tar.gz
参考安装博客:http://blog.topspeedsnail.com/archives/10080
2、计算凸包的例子
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
#include<iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef std::vector<Point_2> Points;//Points是二维数组
int main()
{
Points points, result;
points.push_back(Point_2(10,0));
points.push_back(Point_2(40,40));
points.push_back(Point_2(110,10));
points.push_back(Point_2(60,50));
points.push_back(Point_2(60,30));
CGAL::convex_hull_2( points.begin(), points.end(), std::back_inserter(result) );
for(int i=0;i<result.size();++i)
{
printf("x:%g,y:%g\n",result[i][0],result[i][1]);
}
std::cout << result.size() << " points on the convex hull" << std::endl;
Mat img=Mat::ones(Size(200,100),CV_8UC3);
for(int i=0;i<points.size();i++)
{
cv::circle(img,Point(points[i][0],points[i][1]),0.5,Scalar(0,0,255),0.5,8,0);
}
for(int i=0;i<result.size();++i)
{
if(i==result.size()-1)
{
int j=result.size()-1;
cv::line(img,Point(result[0][0],result[0][1]),Point(result[j][0],result[j][1]),Scalar(255,0,255),1,8,0);
break;
}
else
{
cv::line(img,Point(result[i][0],result[i][1]),Point(result[i+1][0],result[i+1][1]),Scalar(255,0,255),1,8,0);
}
}
cv::namedWindow("img",WINDOW_NORMAL);
cv::imshow("img",img);
cv::waitKey(0);
return 0;
}
CMakeLists
cmake_minimum_required( VERSION 2.8 )
project( Convex_hull_3_Demo )
set( CMAKE_CXX_FLAGS "-std=c++11" )
#cgal
find_package(CGAL REQUIRED)
include(${CGAL_USE_FILE})
#opencv
set(OpenCV_DIR /usr/local/opencv3.4.3/share/OpenCV)
# 寻找OpenCV库
find_package( OpenCV REQUIRED )
# 添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(testCgal main.cpp)
target_link_libraries(testCgal ${CGAL_LIBS} ${OpenCV_LIBS})