系統環境:Ubuntu-14.04
1.安裝g++
$ sudo apt-get install g++
2. 安裝boost
下載下傳boost源檔案【官網】
$tar -zxvf boost_1_58_0.tar.gz
$ cd boost_58_0
$ ./boostrap.sh
$ ./b2 install
3. 安裝cmake
下載下傳cmake源檔案【官網】
$tar -zxvf cmake-3.3.2.tar.gz
$ cd cmake-3.3.2
$ ./bootstrap
$ make
$ make install
4.安裝相關依賴庫
$sudo apt-get install libgmp-dev libmpfr-dev
$sudo apt-get install libgmp-dev
5.安裝CGAL
下載下傳CGAL源檔案【官網】
$ tar -zxvf CGAL_4.6.3.tar.gz
$ cd CGAL_4.6.3
$ cmake .
$ make
$ sudo make install
注:如果報錯OSError: libCGAL.so.11: cannot open shared object file: No such file or directory,解決方法:添加libCGAL.so路徑,如:export LD_LIBRARY_PATH=/usr/local/lib; 或者在/etc/profiles檔案中添加。
eg.
#include <iostream>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/Polygon_2_algorithms.h>
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
using std::cout; using std::endl;
int main(){
Point points[] = { Point(0,0), Point(2,0), Point(2,1), Point(0,1)};
// Point points2[] = { Point(0.5,0.5), Point(1.5,0.5), Point(1.5,1.5), Point(0.5,1.5)};
Point points2[] = { Point(0,0), Point(3,0), Point(3,2), Point(1,0.5)};
Polygon_2 poly1(points, points+4);
Polygon_2 poly2(points2, points2+4);
//CGAL::General_polygon_with_holes_2<K> poly3;
std::list<Polygon_with_holes_2> polyI;
double p1_area = poly1.area();
double p2_area = poly2.area();
CGAL::intersection(poly1, poly2, std::back_inserter(polyI));
double totalArea = 0;
typedef std::list<Polygon_with_holes_2>::iterator LIT;
for(LIT lit = polyI.begin(); lit!=polyI.end(); lit++){
totalArea+=lit->outer_boundary().area();
}
cout << "poly1Area::" << p1_area <<endl
<< "poly2Area::" << p2_area <<endl
<< "IntersetArea::" << totalArea << endl
<< "OverlabArea::" << totalArea*1.0/(p1_area+p2_area-totalArea) << endl;
}
編譯指令:g++ -o poly.o polyOverlab2.cpp -L/usr/local/lib/ -lCGAL -lCGAL_Core -lgmp -lmpfr -lboost_system -lboost_filesystem -lboost_regex -lboost_thread
測試:./poly