文章目录
😏1. 项目介绍
项目Github地址:https://github.com/CGAL/cgal
CGAL(Computational Geometry Algorithms Library)是一个开源的计算几何算法库,它提供了一套丰富的数据结构和算法来解决各种计算几何问题。它是一个功能强大、可靠、高效且易于使用的库。
CGAL 提供了广泛的计算几何算法和数据结构,包括但不限于以下领域:
CGAL 使用 C++ 编写,具有良好的可扩展性和可移植性。它还与其他库和工具集成,在计算机图形学、计算机辅助设计、计算机辅助制造、机器人学、仿真和科学计算等领域得到了广泛应用。
😊2. 环境配置
下面进行环境配置:
apt安装的是老版本4.x,建议源码安装,这里我选的5.1.1.
# apt安装
sudo apt install libcgal-dev
# 源码安装
# 依赖
sudo apt install build-essential libboost-all-dev libgmp-dev libmpfr-dev libopencv-dev
从 `https://github.com/CGAL/cgal/releases/tag/v5.1.1` 下载zip
mkdir build
cd build
cmake -DCGAL_HEADER_ONLY=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALLED_PREFIX=../install ..
make
sudo make install
编译运行:
g++ -o main main.cpp -lCGAL -lgmp
./main
😆3. 使用说明
下面进行使用分析:
计算点集的凸包算法示例:
#include <iostream>
#include <vector>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point;
typedef std::vector<Point> PointVector;
int main()
{
// 创建点向量
PointVector points, result;
// 添加一些二维点到点向量中
points.push_back(Point(1, 1));
points.push_back(Point(2, 3));
points.push_back(Point(4, 2));
points.push_back(Point(3, 1));
points.push_back(Point(2, 2));
points.push_back(Point(3, 3));
points.push_back(Point(3, 2));
points.push_back(Point(5, 4));
points.push_back(Point(5, 1));
points.push_back(Point(4, 3));
points.push_back(Point(4, 4));
// 输出点向量
std::cout << "点集 Points:" << std::endl;
for (const auto &p : points)
{
std::cout << "(" << p.x() << ", " << p.y() << ")" << std::endl;
}
// 计算点集的凸包
CGAL::convex_hull_2(points.begin(), points.end(), std::back_inserter(result));
// 确定绘制区域的边界框
double min_x = result[0].x();
double max_x = result[0].x();
double min_y = result[0].y();
double max_y = result[0].y();
// 输出凸包的点坐标
std::cout << "凸包点 Convex Hull Points:" << std::endl;
for (const auto &p : result)
{
std::cout << "(" << p.x() << ", " << p.y() << ")" << std::endl;
min_x = std::min(min_x, p.x());
max_x = std::max(max_x, p.x());
min_y = std::min(min_y, p.y());
max_y = std::max(max_y, p.y());
}
// 在终端用ASCII字符简单绘制
int width = static_cast<int>(max_x - min_x) + 1;
int height = static_cast<int>(max_y - min_y) + 1;
// 创建并初始化绘制区域
std::vector<std::vector<char>> canvas(height, std::vector<char>(width, '.'));
// 在绘制区域上绘制点
for (const auto& p : result)
{
int x = static_cast<int>(p.x() - min_x);
int y = static_cast<int>(p.y() - min_y);
canvas[y][x] = '#';
}
// 输出绘制结果
std::cout << "绘制结果 #为凸包点: " << std::endl;
for (int y = height - 1; y >= 0; --y)
{
for (int x = 0; x < width; ++x)
{
std::cout << canvas[y][x];
}
std::cout << std::endl;
}
return 0;
}
结果:
点集 Points:
(1, 1)
(2, 3)
(4, 2)
(3, 1)
(2, 2)
(3, 3)
(3, 2)
(5, 4)
(5, 1)
(4, 3)
(4, 4)
凸包点 Convex Hull Points:
(1, 1)
(5, 1)
(5, 4)
(4, 4)
(2, 3)
绘制结果 #为凸包点:
...##
.#...
.....
#...#
以上。