CGAL源码包按安装与初步测试
2024-06-25 本文已影响0人
寽虎非虫003
系统
首先说明下我是在ubuntu20
下操作的。
下载
在github
(https://github.com/CGAL/cgal/releases/tag/v5.6.1)上下载源码包CGAL-5.6.1.tar.xz
。
下载后直接找个文件夹放进去解压即可。
初步使用
建了一个文件夹进行测试
然后从源码包里面拷出来了simple_join_intersect.cpp
和print_utils.h
进行测试,这里simple_join_intersect.cpp
要依赖print_utils.h
进行打印。
simple_join_intersect.cpp
如下
/*! \file simple_join_intersect.cpp
* Computing the union and the intersection of two simple polygons.
*/
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <list>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
typedef std::list<Polygon_with_holes_2> Pwh_list_2;
#include "print_utils.h"
int main ()
{
// Construct the two input polygons.
Polygon_2 P;
P.push_back (Point_2 (0, 0));
P.push_back (Point_2 (5, 0));
P.push_back (Point_2 (3.5, 1.5));
P.push_back (Point_2 (2.5, 0.5));
P.push_back (Point_2 (1.5, 1.5));
std::cout << "P = "; print_polygon (P);
Polygon_2 Q;
Q.push_back (Point_2 (0, 2));
Q.push_back (Point_2 (1.5, 0.5));
Q.push_back (Point_2 (2.5, 1.5));
Q.push_back (Point_2 (3.5, 0.5));
Q.push_back (Point_2 (5, 2));
std::cout << "Q = "; print_polygon (Q);
// Compute the union of P and Q.
Polygon_with_holes_2 unionR;
if (CGAL::join (P, Q, unionR)) {
std::cout << "The union: ";
print_polygon_with_holes (unionR);
} else
std::cout << "P and Q are disjoint and their union is trivial."
<< std::endl;
std::cout << std::endl;
// Compute the intersection of P and Q.
Pwh_list_2 intR;
Pwh_list_2::const_iterator it;
CGAL::intersection (P, Q, std::back_inserter(intR));
std::cout << "The intersection:" << std::endl;
for (it = intR.begin(); it != intR.end(); ++it) {
std::cout << "--> ";
print_polygon_with_holes (*it);
}
return 0;
}
print_utils.h
文件如下
#ifndef CGAL_PRINT_UTILS_H
#define CGAL_PRINT_UTILS_H
#include <CGAL/Polygon_with_holes_2.h>
#include <iostream>
//-----------------------------------------------------------------------------
// Pretty-print a CGAL polygon.
//
template<class Kernel, class Container>
void print_polygon (const CGAL::Polygon_2<Kernel, Container>& P)
{
typename CGAL::Polygon_2<Kernel, Container>::Vertex_const_iterator vit;
std::cout << "[ " << P.size() << " vertices:";
for (vit = P.vertices_begin(); vit != P.vertices_end(); ++vit)
std::cout << " (" << *vit << ')';
std::cout << " ]" << std::endl;
return;
}
//-----------------------------------------------------------------------------
// Pretty-print a polygon with holes.
//
template<class Kernel, class Container>
void print_polygon_with_holes
(const CGAL::Polygon_with_holes_2<Kernel, Container>& pwh)
{
if (! pwh.is_unbounded())
{
std::cout << "{ Outer boundary = ";
print_polygon (pwh.outer_boundary());
}
else
std::cout << "{ Unbounded polygon." << std::endl;
typename CGAL::Polygon_with_holes_2<Kernel,Container>::
Hole_const_iterator hit;
unsigned int k = 1;
std::cout << " " << pwh.number_of_holes() << " holes:" << std::endl;
for (hit = pwh.holes_begin(); hit != pwh.holes_end(); ++hit, ++k)
{
std::cout << " Hole #" << k << " = ";
print_polygon (*hit);
}
std::cout << " }" << std::endl;
return;
}
#endif
然后自己建立了CMakeLists.txt
,里面如下:
cmake_minimum_required(VERSION 3.1...3.23)
project(Boolean_set_polygon_test)
set(CGAL_INCLUDE_DIR ~/PaperDisk/paper/CGAL/CGAL-5.6.1/include)
include_directories(${CGAL_INCLUDE_DIR})
add_executable(Boolean_set_polygon_test simple_join_intersect.cpp)
# target_link_libraries(Boolean_set_polygon_test CGAL::CGAL)
target_link_libraries(Boolean_set_polygon_test gmp mpfr)
整个文件夹结构如下:
.
├── build
├── CMakeLists.txt
├── print_utils.h
└── simple_join_intersect.cpp
编译和运行
在build
文件夹内经过cmake ..
和make
命令后能够正常运行目标文件
./Boolean_set_polygon_test
P = [ 5 vertices: (0 0) (5 0) (3.5 1.5) (2.5 0.5) (1.5 1.5) ]
Q = [ 5 vertices: (0 2) (1.5 0.5) (2.5 1.5) (3.5 0.5) (5 2) ]
The union: { Outer boundary = [ 6 vertices: (1 1) (0 0) (5 0) (4 1) (5 2) (0 2) ]
1 holes:
Hole #1 = [ 4 vertices: (2 1) (2.5 1.5) (3 1) (2.5 0.5) ]
}
The intersection:
--> { Outer boundary = [ 4 vertices: (1 1) (1.5 0.5) (2 1) (1.5 1.5) ]
0 holes:
}
--> { Outer boundary = [ 4 vertices: (3 1) (3.5 0.5) (4 1) (3.5 1.5) ]
0 holes:
}