编译和运行 CGAL 三角测量演示

Compiling and Running CGAL Triangulation Demo

本文关键字:测量 三角 CGAL 运行 编译      更新时间:2023-10-16

我正在尝试使用 CGAL 库来显示 2D Delaunay 三角测量,如这里的例子所示

我的代码如下所示:

#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/draw_triangulation_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Triangulation;
typedef Triangulation::Point Point;
int main(void){
Point a(1,1), b(2,1), c(2,2), d(1,2);
Triangulation T;
T.insert(a);
T.insert(b);
T.insert(c);
T.insert(d);
CGAL::draw(T);
return 0;
}

当我尝试编译此代码时g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp程序成功编译,但在运行时我得到Impossible to draw because CGAL_USE_BASIC_VIEWER is not defined

通过在谷歌上搜索,我发现有人建议使用g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp -DCGAL_USE_BASIC_VIEWER,这在编译时会产生以下错误:/usr/include/CGAL/Qt/Basic_viewer_qt.h:30:10: fatal error: QApplication: No such file or directory #include <QApplication>

我使用的是 ubuntu 19.04,所以我使用sudo apt-get install libcgal-devsudo apt-get install libcgal-qt5-dev安装了 CGAl

我也尝试安装sudo apt-get install libqt5svg5-dev libqt5opengl5-dev来解决错误,但无济于事。

是否需要安装其他库?也许编译必须以不同的方式进行?

谢谢

好的,对于遇到相同问题的任何人来说,这是我如何解决的:

首先,我使用locate QApplication命令查找系统上QApplication头文件的位置。请务必在使用locate之前运行sudo updatedb。如果locate找不到QApplication的位置,那么您就缺少了qt库。尝试sudo apt-get install qt5-default和我在问题中提到的其他库,运行sudo updatedb并再次尝试locate QApplication

找到QApplication路径时,只需使用-I选项指示编译器使用它。这是一个示例g++ -o delaunayTest delaunayTest.cpp -lCGAL -lgmp -lCGAL_Qt5 -DCGAL_USE_BASIC_VIEWER -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/(因为就我而言,QApplication在目录中/usr/include/x86_64-linux-gnu/qt5/QtWidgets/

尝试使用它进行编译,您可能会收到另一个头文件错误。使用locate重复相同的过程,直到不再出现头文件错误。

此时,您可能会遇到undefined reference to symbol错误。

要解决此问题,请再次使用locate查找导致错误的文件的位置(例如libQt5OpenGL.so.5),并按原样将路径添加到编译命令(例如g++ -o delaunayTest delaunayTest.cpp /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so.5)以及所有前面的选项。

您可能还会遇到几个undefined reference to symbol错误。只需继续使用相同的方法,直到您没有得到任何方法。

此时,程序应正确编译并正常运行。

请注意,如果您安装了多个版本的qt,则上述方法可能无法正常工作(例如,如果您的系统中安装了使用QT的软件,例如MATLAB或anaconda。你会知道的,因为locate会在上述步骤中为每个文件生成许多路径)。在这种情况下,我建议构建虚拟机,下载CGAL库和qt5-default并按照上述步骤进行操作,因为这很可能在具有多个qt安装的系统中不起作用。

使用 CMake 的另一种选择(也许是最简单的)是使用内置脚本生成文件:

  1. 从源文件目录中,运行cgal_create_CMakeLists -c Qt5
  2. 编辑生成的CMakeLists.txt添加行add_definitions(-DCGAL_USE_BASIC_VIEWER)

我生成和编辑CMakeLists.txt

# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
cmake_minimum_required(VERSION 3.1...3.15)
project( tmp_cgal )
# CGAL and its components
find_package( CGAL QUIET COMPONENTS Qt5 )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()  
endif()
add_definitions(-DCGAL_USE_BASIC_VIEWER)  # <==== I've added this

# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()  
endif()
# include for local directory
# include for local package

# Creating entries for all C++ files with "main" routine
# ##########################################################

create_single_source_cgal_program( "b.cpp" )

  1. 创建一个build目录:mkdir build
  2. 更改目录:cd build
  3. 生成构建文件:cmake ..
  4. 版本:make
  5. 运行二进制文件。对我来说,这很./b,因为我的源文件b.cpp

环境:

这些说明应该适用于安装了软件包的 Ubuntu 20.04.1(或类似版本),libcgal-devlibcgal-qt5-devqtbase5-dev(当然,还有cmakemakeg++)。

主要参考资料:

文档 1 和文档 2