CGAL计算几何算法库安装和使用(一)

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})

在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值