Article directory
1 CGAL overview
Computational Geometry Algorithms Library (CGAL) is a C++ algorithm library that provides efficient and reliable geometric algorithms.
CGAL provides many data structures and algorithms, such as triangulations , Voronoi diagrams , Polygons , Cell Complexes and Polyhedra , arrangements of curves , nets Lattice generation ( mesh generation ), geometry processing ( geometry processing ), convex hull algorithm ( convex hull algorithms ), etc.
All of these data structures and algorithms operate on geometric objects such as points and line segments and perform geometric tests on them. These objects and predicates are regrouped in CGAL Kernels .
Finally, the Support Library provides geometric object generators and spatial sorting functions, as well as matrix search frameworks and solvers for linear and quadratic programming. It also provides interfaces to third-party software such as the GUI library Qt, Geomview, and the Boost graphics library.
2 Install CGAL from Source Archive
2.1 CGAL download
CGAL-5.3.1.zip download address: https://github.com/CGAL/cgal/releases
Unzip the downloaded file CGAL-5.3.1.zip
and put it in a certain path, here it is placed in the following path
D:\Program Files\CGAL-5.3.1
2.2 GMP and MPRF installation
Precompiled versions of GMP and MPFR are available in Assets for Windows 64-bit. If we install these libraries just to use CGAL, then we should CGAL-5.3.1-win64-auxiliary-libraries-gmp-mpfr.zip
unzip the files, and then auxiliary
copy the folders inside them to CGAL-5.3.1
the unzip directory.
Since the CGAL-5.3.1
directory already contains a auxiliary
folder, it can be replaced directly here.
2.3 boost installation and environment variable settings
2.3.1 boost installation
Boost is a mandatory dependency of CGAL. SourceForge provides a binary version of Boost (this version can be used directly without compiling). The Boost installer installs both the Boost headers and precompiled libraries. When CGAL 5.3.1 was released, the latest version of Boost was 1.71 (as of January 20, 2022, the latest version of Boost was 1.78.0).
VS | boost |
---|---|
vs2015 | boost-msvc-14.0 |
vs2017 | boost-msvc-14.1 |
vs2019 | boost-msvc-14.2 |
Taking VS2017 to install Boost1.78.0 as an example , to install boost, the steps are as follows:
1) Download and run boost_1_78_0-msvc-14.1-64.exe
the file, select the installation directory
2) Click Next>
to install...
3) Click Finish
to complete the installation.
2.3.2 Environment variable settings
Open Control Panel, search for " Advanced ", select "View advanced system settings " under " System "
Select " Environment Variables "
if you want to set this user, select New in User Variables; if you want to be valid for all users, select System Variables.
Both options operate the same. Here's an example of " user variables ":
Under User Variables, select " New ", and the " New User Variable " dialog box will pop up.
Set the variable name and variable value in turn.
BOOST_LIBRARYDIR = D:\Program Files\libboost_1_78_0\lib64-msvc-14.1
BOOST_INCLUDEDIR = D:\Program Files\libboost_1_78_0
Finally, add the path of the boost dll file (subject to your own path) to the path environment variable: double-click " Path " in " User Variables ", and the " Edit Environment Variables dialog box " will pop up, click " New ", and paste path to boost dll file
D:\Program Files\boost_1_78_0\lib64-msvc-14.1
3 VS property sheet configuration
1) Open vs2017 and click "File->New->New Project->Empty Project" in the upper left corner
2) Select "Property Manager -> Debug | x64", right-click "Add New Project Property Sheet" (once and for all, you can call it directly when creating a new project in the future, no need to reconfigure)
3) Double-click the newly created property sheet, add the content
Add the following path to "Common Properties->VC++ Directories->Include Directories"
D:\Program Files\CGAL-5.3.1\include
D:\Program Files\CGAL-5.3.1\auxiliary\gmp\include
D:\Program Files\boost_1_78_0
4) Add library directory
Add the following path to "Common Properties->VC++ Directories->Library Directories"
D:\Program Files\CGAL-5.3.1\auxiliary\gmp\lib
D:\Program Files\boost_1_78_0\libs
5) Add additional dependencies (lib files under D:\Program Files\CGAL-5.3.1\auxiliary\gmp\lib)
Paste the following filename into " Common Properties -> Linker -> Input > Additional Dependencies "
libgmp-10.lib
libmpfr-4.lib
4 Test code
Code:
#include <iostream>
#include <CGAL/Simple_cartesian.h> //笛卡尔坐标相关头文件
using namespace std;
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Segment_2 Segment_2;
int main()
{
//定义两个二维点
Point_2 p(1, 1), q(10, 10);
cout << "p = " << p << endl;
cout << "q = " << q.x() << " " << q.y() << endl;
//两点间的平方距离
double sqDist_pq;
sqDist_pq = CGAL::squared_distance(p, q);
cout << "->两点间的平方距离:"<< CGAL::squared_distance(p, q) << endl;
//两点的中点
cout << "->两点间的中点:" << CGAL::midpoint(p, q) << endl;
//两点确定一条直线
Segment_2 s(p, q);
Point_2 m(5, 9);
cout << "m = " << m << endl;
//点到直线的距离
double sqDist_sm;
sqDist_sm = CGAL::squared_distance(s, m);
cout << "->点到直线的距离" << sqDist_sm << endl;
//判断三点的关系
cout << "p, q, 和 m 三点的关系为:";
switch (CGAL::orientation(p, q, m))
{
case CGAL::COLLINEAR:
cout << "三点共线\n";
break;
case CGAL::LEFT_TURN:
cout << "三点构成左转\n";
break;
case CGAL::RIGHT_TURN:
cout << "三点构成右转\n";
break;
}
return 0;
}
Output result:
p = 1 1
q = 10 10
->两点间的平方距离:162
->两点间的中点:5.5 5.5
m = 5 9
->点到直线的距离8
->p, q, 和 m 三点的关系为:三点构成左转