CGAL的安装与在VS中的配置

CGAL的安装与在VS中的配置

参考:CGAL编译与使用(Windows) · 语雀 (yuque.com)

CGAL+VS+Qt环境配置_balduck的博客-CSDN博客_cgal qt

从5.0版本开始,CGAL仅是一个头文件库,这意味着CGAL无需编译,只需安装好CGAL的依赖项即可。

  • CGAL主要依赖项是boost
  • CGAL还依赖GMP和MPFR(CGAL会提供编译好的)
  • 如果需要使用CGAL的可视化功能,则还需要Qt库

源码安装

CGAL5.4.1+Windows+VS2022+QT5.13.1

安装Boost

Boost是CGAL的强制依赖库。下载地址

  1. 下载boost_1_79_0-msvc-14.3-64.exe image-20220704130447420

  2. 运行下载器,安装到D:\local\boost_1_79_0

  3. 设置环境变量,这帮助cmake找到boost

    • BOOST_LIBRARYDIR = D:\local\boost_1_79_0\lib64-msvc-14.3
    • Boost_INCLUDEDIR = D:\local\boost_1_79_0
    • PATH = D:\local\boost_1_79_0\lib64-msvc-14.3

安装CGAL

官网下载

image-20220704201601645

下载结束后解压,把GMP那个包放到CGAL包对应目录下就行(目录:\CGAL-5.4.1\auxiliary

然后切记将gmp的lib路径添加到环境变量path中!!!

配置环境变量

CGAL_DIR = D:\CGAL\CGAL-5.4.1,帮助cmake在配置过程中找到CGAL

安装Qt

访问官网进行下载,建议安装QT5.13.1

选择安装组件

image-20220705003449987

添加环境变量

  1. QTDIR = D:\local\Qt\Qt5.13.1,帮助cmake找到Qt
  2. PATH = D:\local\Qt\Qt5.13.1\5.13.1\msvc2017_64\bin。为了避免与另一个文件夹中具有相同名称的另一个dll发生任何冲突,请将此路径添加为列表中的第一个。

在VS中安装Qt插件

  1. 打开VS2022,拓展–>管理拓展–>联机–>搜索Qt–>安装Qt Visual Studio Tools
  2. 安装完成后,重启VS,点集文件->新建->项目->Qt就可以关于Qt的信息;并且在工具栏中会有Qt VS Tools选项

VS2022配置

新建项目属性表,并进行以下配置

1 添加包含目录(通用属性->VC++ 目录->包含目录

image-20220704203325134

2 添加库目录(通用属性->VC++ 目录->库目录

image-20220704203807790

CGAL只是一个头文件夹包,故只要引用头文件即可

3 添加依赖项(通用属性->链接器->输入>附加依赖项

image-20220704204637486

libgmp-10.lib
libmpfr-4.lib

测试

测试代码1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <CGAL/Simple_cartesian.h>
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);
std::cout << "p = " << p << std::endl;
std::cout << "q = " << q.x() << " " << q.y() << std::endl;
std::cout << "sqdist(p,q) = "
<< CGAL::squared_distance(p, q) << std::endl;
Segment_2 s(p, q);
Point_2 m(5, 9);
std::cout << "m = " << m << std::endl;
std::cout << "sqdist(Segment_2(p,q), m) = "
<< CGAL::squared_distance(s, m) << std::endl;
std::cout << "p, q, and m ";
switch (CGAL::orientation(p, q, m)) {
case CGAL::COLLINEAR:
std::cout << "are collinear\n";
break;
case CGAL::LEFT_TURN:
std::cout << "make a left turn\n";
break;
case CGAL::RIGHT_TURN:
std::cout << "make a right turn\n";
break;
}
std::cout << " midpoint(p,q) = " << CGAL::midpoint(p, q) << std::endl;
return 0;
}

image-20220704224425280

测试代码2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Polygon_set_2.h>
#include <CGAL/draw_polygon_set_2.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
typedef CGAL::Polygon_set_2<K> Polygon_set_2;
typedef CGAL::Point_2<K> Point_2;
Polygon_2 rectangle(int l)
{
// Create a rectangle with given side length.
Polygon_2 P;
P.push_back(Point_2(-l, -l));
P.push_back(Point_2(l, -l));
P.push_back(Point_2(l, l));
P.push_back(Point_2(-l, l));
return P;
}
int main()
{
// Create a large rectangle A, with a hole and a smaller rectangle
// B inside A's hole.
Polygon_with_holes_2 A(rectangle(3));
Polygon_2 H(rectangle(2));
H.reverse_orientation();
A.add_hole(H);
Polygon_2 B(rectangle(1));
// Add them to a polygon set and draw it.
Polygon_set_2 S;
S.insert(A);
S.insert(B);
CGAL::draw(S);
return 0;
}

可能报错:

image-20220704224956578

打开属性页,在C/C++命令行中加/bigobj

image-20220704225224656

image-20220704225510632

没有配置qt的,导致上图结果

找到项目目录,创建CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# This is the CMake script for compiling a CGAL application.

cmake_minimum_required(VERSION 3.1...3.22)
project(CGAL_TEST)

find_package(CGAL REQUIRED OPTIONAL_COMPONENTS Qt5)

# create a target per cppfile
file(
GLOB cppfiles
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
foreach(cppfile ${cppfiles})
create_single_source_cgal_program("${cppfile}")
endforeach()

if(CGAL_Qt5_FOUND)
target_link_libraries(main PUBLIC CGAL::CGAL_Basic_viewer)
else()
message(
STATUS
"NOTICE: The example draw_triangulation_2 requires Qt and will not be compiled."
)
endif()

使用cmake生成工程文件

  1. 打开cmake的图形界面
  2. where is the source code:例:D:\local\CGAL-5.0.2\examples\Triangulation_2
  3. where to build the binaries:例:D:\local\CGAL-5.0.2\examples\Triangulation_2\build
  4. Configure->VS 17 2022,Win64->Finish
  5. Generate

运行

  1. 打开生成的CGAL_TEST.sln文件(目录位置:D:\local\CGAL-5.0.2\examples\Triangulation_2\build
  2. 选择要编译的例子: 将main设置为启动项
  3. 运行即可

运行结果如下:

image-20220705132310224

至此,CGAL的环境彻底配置完毕!


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!