GCAL几何算法库,涵盖了很多数学几何算法,矩阵运算、平面拟合、曲线拟合、曲面重建、网格优化、网格剖分、面线相交、布尔运算等等各种图形学几何相关的算法。
文章最后放了一个CGAL求点集拟合平面,投影求线的例子代码。
CGAL是一个开源代码库,官网连接GitHub - CGAL/cgal: The public CGAL repository, see the README below
1.CGAL下载
GitHub下载源码,网速慢的可以私信我要
CGAL不需要编译,是完全基于头文件使用的几何功能库,依赖的第三方库也在官网可以下载
2.CGAL使用例子
CMake 编译使用例子程序,还需要一些渲染界面依赖安装
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()));
}