三维图形程序员必学-CGAL几何算法

本文介绍了CGAL几何算法库,包括其功能如矩阵运算、拟合等,并提供了求三维点集拟合平面及投影的C++代码示例,展示了如何使用CMake进行编译和依赖管理。
摘要由CSDN通过智能技术生成

GCAL几何算法库,涵盖了很多数学几何算法,矩阵运算、平面拟合、曲线拟合、曲面重建、网格优化、网格剖分、面线相交、布尔运算等等各种图形学几何相关的算法。

文章最后放了一个CGAL求点集拟合平面,投影求线的例子代码。

CGAL是一个开源代码库,官网连接GitHub - CGAL/cgal: The public CGAL repository, see the README belowcf2fc72a4c1b40c98d7c8eb913eb84b3.png

1.CGAL下载

GitHub下载源码,网速慢的可以私信我要

36beb8e296094104b0c50e38a61b7d35.png

CGAL不需要编译,是完全基于头文件使用的几何功能库,依赖的第三方库也在官网可以下载

2.CGAL使用例子

6e3dbb031b71406793a7ce350e4c3ca0.png

 

CMake 编译使用例子程序,还需要一些渲染界面依赖安装

9fb49680f2de4fa7a462de3ef1bd6087.png

 

3.简单的求三维点集拟合平面的例子代码

	std::vector<K::Point_3> cgal_p1;
	K::Plane_3 plane;
	CGAL::linear_least_squares_fitting_3(cgal_p1.begin(), cgal_p1.end(), plane, CGAL::Dimension_tag<0>());

	K::Point_3 pointA(center.x, center.y, center.z);


	K::Vector_3 normalToKnownPlane = plane.orthogonal_vector();

	K::Vector_3 normalToB = K::Vector_3(1, -normalToKnownPlane.x() / normalToKnownPlane.y(), 0); // 任意选择一个垂直于 A 的向量


	K::Vector_3 perpendicular_vector = CGAL::cross_product(normalToKnownPlane, normalToB);

	normal.x = perpendicular_vector.x();
	normal.y = perpendicular_vector.y();
	normal.z = perpendicular_vector.z();


	K::Plane_3  requiredPlane(pointA, perpendicular_vector);

	std::vector<K::Point_3> cgal_project_p1;
	for (const K::Point_3& point : cgal_p1) {
		// 计算点到平面的投影
		K::Line_3 line(point, requiredPlane.orthogonal_vector());
		auto intersection = CGAL::intersection(requiredPlane, line);
		if (intersection) {
			const K::Point_3* intersectionPoint = boost::get<K::Point_3>(&*intersection);
			if (intersectionPoint) {
				cgal_project_p1.push_back(*intersectionPoint);
			}
		}
	}

	std::vector<Point3f> project_rp1;
	for (const K::Point_3& point : cgal_project_p1) {
		project_rp1.push_back(Point3f(point.x(), point.y(), point.z()));
	}

 

 

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老猿的春天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值