3D曲面上的TSP问题(一):曲面上点集距离求解

3D曲面上,两点的距离求解不能采用欧式距离,而需要计算测地线距离

代码使用CGAL 5.6.2 + OpenCV 4.11.0 版本实现

cpp 复制代码
#include "cgal_utils.h"
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
#include <CGAL/Surface_mesh_shortest_path.h>
#include <CGAL/boost/graph/graph_traits_Surface_mesh.h>

// 将CGAL点转换为glm::dvec3
glm::dvec3 cgal_utils::cgalToGLM(const cgal_utils::Point_3& p) {
	return glm::dvec3(CGAL::to_double(p.x()),
		CGAL::to_double(p.y()),
		CGAL::to_double(p.z()));
}

// 将glm::dvec3转换为CGAL点
cgal_utils::Point_3 cgal_utils::glmToCGAL(const glm::dvec3& p) {
	return cgal_utils::Point_3(p.x, p.y, p.z);
}

// 将glm::dvec3转换为CGAL点
std::vector<cgal_utils::Point_3> cgal_utils::convertToCGALPoints(const std::vector<glm::dvec3>& points) {
	std::vector<Point_3> cgalPoints;
	cgalPoints.reserve(points.size());
	for (const auto& p : points) {
		cgalPoints.emplace_back(p.x, p.y, p.z);
	}
	return cgalPoints;
}

// 计算凸包并返回Polyhedron
cgal_utils::Polyhedron_3 cgal_utils::computeConvexHullAsPolyhedron(const std::vector<glm::dvec3>& inputPoints) 
{
	// 转换点格式
	std::vector<cgal_utils::Point_3> points = convertToCGALPoints(inputPoints);

	// 计算凸包
	cgal_utils::Polyhedron_3 poly;
	CGAL::convex_hull_3(points.begin(), points.end(), poly);

	return poly;
}

// 计算凸包并返回Surface_mesh
cgal_utils::Surface_mesh cgal_utils::computeConvexHullAsSurfaceMesh(const std::vector<glm::dvec3>& inputPoints) {
	// 转换点格式
	std::vector<cgal_utils::Point_3> points = convertToCGALPoints(inputPoints);

	// 计算凸包
	cgal_utils::Surface_mesh mesh;
	CGAL::convex_hull_3(points.begin(), points.end(), mesh);

	return mesh;
}

// 保存Polyhedron到OFF文件
void cgal_utils::savePolyhedronToOFF(const cgal_utils::Polyhedron_3& poly, const std::string& filename) {
	std::ofstream out(filename);
	out << poly;
	out.close();
}

// 保存Surface_mesh到OFF文件
void cgal_utils::saveSurfaceMeshToOFF(const cgal_utils::Surface_mesh& mesh, const std::string& filename) {
	std::ofstream out(filename);
	out << mesh;
	out.close();
}

// 计算每个点到凸包的最近点
std::vector<glm::dvec3> cgal_utils::computeClosestPointsOnHull(
	const std::vector<glm::dvec3>& inputPoints,
	const cgal_utils::Surface_mesh& convexHull) {

	// 创建AABB树用于最近点查询
	typedef CGAL::AABB_face_graph_triangle_primitive<Surface_mesh> Primitive;
	typedef CGAL::AABB_traits<K, Primitive> Traits;
	typedef CGAL::AABB_tree<Traits> Tree;
    
    	Tree tree(faces(convexHull).first, faces(convexHull).second, convexHull);
	tree.accelerate_distance_queries();

	std::vector<glm::dvec3> closestPoints;
	closestPoints.reserve(inputPoints.size());

	for (const auto& p : inputPoints) {
		Point_3 query = cgal_utils::glmToCGAL(p);
		Point_3 closest = tree.closest_point(query);
		if (isnan(closest.x()))
			std::cout << "nan" << std::endl;
		closestPoints.push_back(cgalToGLM(closest));
	}

	return closestPoints;
}

// 计算测地线距离矩阵
cv::Mat cgal_utils::computeGeodesicDistances(
	const std::vector<glm::dvec3>& points,
	const cgal_utils::Surface_mesh& mesh) {

    // 创建距离矩阵
cv::Mat distanceMatrix(points.size(), points.size(), CV_64F);

// 创建最短路径计算对象
typedef CGAL::Surface_mesh_shortest_path_traits<K, Surface_mesh> Traits;
typedef CGAL::Surface_mesh_shortest_path<Traits> Shortest_path;
typedef typename Shortest_path::Face_location              Face_location;

typedef CGAL::AABB_face_graph_triangle_primitive<Surface_mesh>     AABB_face_graph_primitive;
typedef CGAL::AABB_traits<K, AABB_face_graph_primitive>            AABB_face_graph_traits;
typedef CGAL::AABB_tree<AABB_face_graph_traits>                    AABB_tree;

Shortest_path shortest_paths(mesh);

auto checkLoc = [](Face_location& loc)
	{
		if(std::isnan(loc.second[0]) || std::isinf(loc.second[0]))
			return false;
		return true;
	};

// 为每个点找到最近的顶点、边或面
std::vector<Face_location> locations;
for (const auto& p : points) {
	cgal_utils::Point_3 query = cgal_utils::glmToCGAL(p);
	auto loc = shortest_paths.locate<AABB_face_graph_traits>(query);
    // 这里有一些困惑,会存在nan或inf值的问题,临时的解决方案是
    // 发现这样的值之后,让原来的点稍稍偏一点距离,再重新求一遍;
    // 这里为了以防万一,XYZ轴各试一遍,直到值正常了就不再试了
	if (!checkLoc(loc))
    		{
			query = cgal_utils::glmToCGAL(p + glm::dvec3(0.001, 0, 0));
			loc = shortest_paths.locate<AABB_face_graph_traits>(query);
			if (!checkLoc(loc))
			{
				query = cgal_utils::glmToCGAL(p + glm::dvec3(0, 0.001, 0));
				loc = shortest_paths.locate<AABB_face_graph_traits>(query);
				if (!checkLoc(loc))
				{
					query = cgal_utils::glmToCGAL(p + glm::dvec3(0, 0, 0.001));
					loc = shortest_paths.locate<AABB_face_graph_traits>(query);
				}
			}
		}
		locations.push_back(loc);
	}

	// 计算所有点对之间的测地线距离
	for (size_t i = 0; i < points.size(); ++i) {
		shortest_paths.add_source_point(locations[i]);

		for (size_t j = 0; j < points.size(); ++j) {
			if (i == j) {
				distanceMatrix.at<double>(i, j) = 0.0;
			}
			else {
				double dist = CGAL::to_double(shortest_paths.shortest_distance_to_source_points(locations[j].first, locations[j].second).first);
				if (dist < 0)
					dist = 100000;
				distanceMatrix.at<double>(i, j) = dist;
			}
		}

				shortest_paths.clear();
	}

	return distanceMatrix;
}

cv::Mat cgal_utils::computeConvexHullAndGeodesicDistances(const std::vector<glm::dvec3>& points)
{
	auto mesh = computeConvexHullAsSurfaceMesh(points);
	//saveSurfaceMeshToOFF(mesh, "convex_hull.off");
	auto nearestPts = computeClosestPointsOnHull(points, mesh);
	cv::Mat result = computeGeodesicDistances(nearestPts, mesh);
	return result;
}
相关推荐
多恩Stone16 小时前
【3D-AICG 系列-2】Trellis 2 的O-voxel (上) Shape: Flexible Dual Grid
人工智能·python·算法·3d·aigc
syncon121 天前
手机被划了一道白色痕迹怎么修复?-TFT-LCD液晶激光修复
科技·3d·制造
charley.layabox1 天前
LayaAir3.4性能大幅提升、新增IK、动画引导线、3D Spine、动态合并图集、帧调试器、Linux版本IDE、完善WebGPU等
3d·spine
TTGGGFF1 天前
概念解析:机器视觉如何赋予机器“三维双眼”——3D重建技术全景指南
3d
新启航光学频率梳1 天前
燃料电池电堆极板流场深孔孔深3D轮廓测量-激光频率梳3D轮廓技术
科技·3d·制造
Qt学视觉1 天前
3D3-PCL全面总结
c++·opencv·3d
Aevget2 天前
全面进化的工程级 3D 可视化 SDK:HOOPS Visualize Desktop 2026.1.0正式发布
3d·hoops·3d渲染·3d数据可视化·3d数据格式转化
多恩Stone2 天前
【3DV 进阶-11】Trellis.2 数据处理与训练流程图
人工智能·pytorch·python·算法·3d·aigc·流程图
ejinxian2 天前
谷歌发布 Project Genie:基于文本生成可互动 3D 虚拟世界
人工智能·3d·project genie
CG_MAGIC2 天前
3ds Max场景烘焙:大型建筑/道具的光照贴图批量生成
3d·blender·贴图·zbrush·建模教程·渲云渲染