目录
1. 必要的说明
1.1 简介
CGAL 的全称是 Computational Geometry Algorithms Library,计算几何算法库。它是使用 C++ 语言编写的开源软件,它被广泛应用于几何计算相关的领域,提供众多计算几何相关的数据结构与算法诸如三角剖分,Voronoi 图,多边形,多面体,曲线整理及其应用,网格生成,几何处理,凸壳算法,搜索结构,插值,形状分析,拟合,距离等。当前版本 5.1。阅读本文前也可大致浏览下
使用CGAL需提前安装如下核心组件:
- C++ 编译器
- CMake
- Boost
- GMP 和 MPFR
- Qt5(开发时用)
1.2 软件安装说明
根据基础情况不同,软件安装顺序如下:
-
当你已有 g++ 6.3.0 及以上版本时,安装从左至右依次进行。
1 2 3 4 5 6 GMP MPFR Boost CMake Qt5 CGAL -
当现有版本低于 g++ 6.3.0 且想源码编译安装高版本 g++ 时,还需软件 MPC ,从左至右依次安装。
1 2 3 4 5 6 7 8 GMP MPFR MPC GCC Boost CMake Qt5 CGAL
本文是在第 2 种情况下进行安装的。为啥要强调顺序呢?因为他们有较强的依赖关系:
- GCC 编译依赖于GMP,MPFR 和 MPC
- MPFR 依赖于GMP
- MPC 依赖于 GMP 和 MPFR
- CGAL 的很多 Examples 又需要Qt5的支持
2. 依赖软件安装
2.1 GMP & MPFR & MPC & GCC
这部分内容篇幅较长拆出,见如下
2.2 Boost
2.3 CMake
2.4 Qt
还没安装图形化界面的童鞋请看这里,如果使用的是 Tencent Cloud 或 Aliyun 远程服务器的话,使用VNC连接远程主机更方便。
本文直接使用图形化界面安装 Qt,执行如下命令后可视化安装。
[root@VM-0-9-centos home]# mkdir qt
[root@VM-0-9-centos home]# cd qt
[root@VM-0-9-centos qt]# wget http://download.qt.io/official_releases/qt/5.14/5.14.0/qt-opensource-linux-x64-5.14.0.run
[root@VM-0-9-centos qt]# chmod +x qt-opensource-linux-x64-5.14.0.run
[root@VM-0-9-centos qt]# ./qt-opensource-linux-x64-5.14.0.run
完成后就可以开始正式安装 CGAL 吧。
3. CGAL
按常理先建目录,再下载解压,如下
[root@VM-0-9-centos home]# mkdir /home/cgal
[root@VM-0-9-centos home]# cd cgal
[root@VM-0-9-centos cgal]# wget https://github.com/CGAL/cgal/releases/download/v5.1/CGAL-5.1.tar.xz
[root@VM-0-9-centos cgal]# tar -xvf CGAL-5.1.tar.xz
[root@VM-0-9-centos cgal]# cd CGAL-5.1
由于 CGAL 5.0 是默认的仅header格式,做戏做全套,我们不用这种默认方式。创建 build/
目录后在该目录下cmake
,注意有两个 . 点儿。
[root@VM-0-9-centos CGAL-5.1]# mkdir build
[root@VM-0-9-centos CGAL-5.1]# cd build
[root@VM-0-9-centos build]# cmake -DCGAL_HEADER_ONLY=OFF -DCMAKE_BUILD_TYPE=Release ..
重点来了啊,上面这一步的输出一定要好好看,有任何一个错误都是以后的麻烦。正确输出应有如下
[root@VM-0-9-centos build]# cmake -DCGAL_HEADER_ONLY=OFF -DCMAKE_BUILD_TYPE=Release ..
...
== Generate version files ==
...
########## 1. 看 c++ 版本是不是安装的最新的, 版本低会导致失败 ##########
-- Using /usr/local/bin/c++ compiler.
-- USING CMake version: 3.19.0
-- System: Linux
-- USING GCC_VERSION = '10.2.0'
== Generate version files (DONE) ==
...
########## 2. 开始核验依赖库情况 ##########
-- External libraries supported: GMP;MPFR;ZLIB;OpenGL;LEDA;MPFI;RS;RS3;OpenNL;Eigen3;ESBTL;Coin3D;NTL;IPE
########## 3. 看 gmp 的 头/库 文件的路径及版本号是否填充且是正确的 ##########
-- Preconfiguring library: GMP ...
-- GMP has been preconfigured:
-- UseGMP-file:
-- GMP include: /usr/local/include
-- GMP libraries: /usr/local/lib/libgmp.so
-- GMP definitions:
-- USING GMP_VERSION = '6.2.0'
########## 4. 看 mpfr 的 头/库 文件的路径及版本号是否填充且是正确的 ##########
-- Preconfiguring library: MPFR ...
-- MPFR has been preconfigured:
-- UseMPFR-file:
-- MPFR include: /usr/local/include
-- MPFR libraries: /usr/local/lib/libmpfr.so
-- MPFR definitions:
-- USING MPFR_VERSION = '4.1.0'
########## 5. 看boost,这个有头就行 ##########
-- Boost include dirs: /usr/local/include
-- Boost libraries:
== Detect external libraries (DONE) ==
== Write compiler_config.h ==
...
== Write compiler_config.h (DONE) ==
== Generating build files ==
########## 6. 看 gmp,mpfr,boost 应找到对应的版本了 ##########
Configuring libCGAL
-- Using gcc version 4 or later. Adding -frounding-math
-- USING GMP_VERSION = '6.2.0'
-- USING MPFR_VERSION = '4.1.0'
-- USING BOOST_VERSION = '1.74.0'
libCGAL is configured
-- Sources for CGAL component library 'CGAL_Core' detected
Configuring libCGAL_Core
########## 7. CGAL_Core 配置完成 ##########
libCGAL_Core is configured
-- Sources for CGAL component library 'CGAL_ImageIO' detected
Configuring libCGAL_ImageIO
-- USING ZLIB_VERSION = '1.2.7'
########## 8. CGAL_ImageIO 配置完成 ##########
libCGAL_ImageIO is configured
-- Sources for CGAL component library 'CGAL_Qt5' detected
Configuring libCGAL_Qt5
-- USING Qt5_VERSION = '5.14.0'
########## 9. CGAL_Qt5 配置完成,非常重要,容易失败(大概率由于没添加环境变量) ##########
libCGAL_Qt5 is configured
-- Sources for CGAL component libraries 'CGAL_Core;CGAL_ImageIO;CGAL_Qt5' detected
== Generating build files (DONE) ==
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cgal/CGAL-5.1/build
然后执行
[root@VM-0-9-centosbuild]# make
[root@VM-0-9-centos build]# make install
4. 测试
4.1 构建一个Example
[root@VM-0-9-centos build]# cd ../Triangulation_3/
[root@VM-0-9-centos Triangulation_2]# cmake -DCMAKE_BUILD_TYPE=Release .
-- Found Boost: /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake (found version "1.74.0")
-- Found Boost: /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.48")
-- Boost include dirs: /usr/local/include
-- Boost libraries:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cgal/CGAL-5.1/examples/Triangulation_2
[root@VM-0-9-centos Triangulation_2]# make
在图形界面下执行,弹出图形窗口,运行成功
[root@VM-0-9-centos Triangulation_2]# ./draw_triangulation_2
4.2 使用Qt编译一个CGAL项目
创建 名为 cgaltest2 的 Qt 控制台项目,其中 main.cpp
如下
#include <QCoreApplication>
#define CGAL_USE_BASIC_VIEWER
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/draw_triangulation_3.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_3<K> DT3;
typedef CGAL::Creator_uniform_3<double,K::Point_3> Creator;
int main()
{
std::vector<K::Point_3> points;
CGAL::Random_points_in_sphere_3<K::Point_3,Creator> g(1.0);
std::copy_n(g, 50, std::back_inserter(points));
DT3 dt3(points.begin(), points.end());
CGAL::draw(dt3);
return EXIT_SUCCESS;
}
cgaltest2.pro
如下
QT -= gui
QT += widgets
QT += xml
QT += opengl
CONFIG += c++14 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
LIBS += -L/usr/local/lib64/ -lgmp
LIBS += -L/usr/local/lib64/ -lCGAL_Qt5
编译运行。
5. 可能出现问题
5.1 CGAL_USE_BASIC_VIEWER is not defined.
Qt图形程序编译完成,执行后出现如下错误
Impossible to draw, CGAL_USE_BASIC_VIEWER is not defined.
解决办法
需对其进行定义,在 main.cpp
前添加
#define CGAL_USE_BASIC_VIEWER
5.2 undefined reference to `__gmpz_mul’
编译 Qt 项目时出现错误如下
/usr/local/include/gmpxx.h:496: error: undefined reference to `__gmpz_mul'
解决办法
需对其进行定义,在 .pro
前添加(找到自己机器下该文件路径)
LIBS += -L/usr/local/lib64/ -lgmp
这种问题出现的较多,还有类似于
/usr/local/include/CGAL/Qt/frame.h:133: error: undefined reference to `vtable for CGAL::qglviewer::Frame'```
需添加
LIBS += -L/usr/local/lib64/ -lCGAL_Qt5
5.3 Killed signal terminated program cc1plus
编译 Qt 项目时出现错误如下
:-1: error: fatal error: Killed signal terminated program cc1plus
解决办法
常出现在云服务器中,是由于内存不足造成的。可通过设置2G交换分区来解决该问题。
#获取要增加的2G的SWAP文件块
dd if=/dev/zero of=/swapfile bs=1k count=2048000
#创建SWAP文件
mkswap /swapfile
#激活SWAP文件
swapon /swapfile
#查看SWAP信息是否正确
swapon -s
#添加到fstab文件中让系统引导时自动启动
echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab
6. 参考
[1] https://doc.cgal.org/latest/Manual/usage.html
[2] https://www.lxx1.com/3886
.
.
.
.
.
.
桃花仙人种桃树,又摘桃花换酒钱_