/* geodoer@Time: 2021/8/20@desc: 多个表面模型,合并成一个*/#include <CGAL/Exact_predicates_exact_constructions_kernel.h>#include <CGAL/Polyhedron_3.h>#include <CGAL/Surface_mesh.h>#include <CGAL/Nef_polyhedron_3.h>#include <CGAL/boost/graph/convert_nef_polyhedron_to_polygon_mesh.h>#include<vector>#include<QDir>#include<QTime>//将PolygonSoup转PolygonMesh#include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>#include <CGAL/Polygon_mesh_processing/orientation.h>#include <CGAL/IO/polygon_soup_io.h>typedef CGAL::Exact_predicates_exact_constructions_kernel Exact_kernel;typedef Exact_kernel::Point_3 Point;typedef CGAL::Polyhedron_3<Exact_kernel> Polyhedron;typedef CGAL::Surface_mesh<Exact_kernel::Point_3> Surface_mesh;typedef CGAL::Nef_polyhedron_3<Exact_kernel> Nef_polyhedron;using namespace std;Polyhedron read_off(const std::string& dir, const std::string& fn){auto fp = dir + "\\" + fn;//表面模型不一定是PolygonMesh,只是PolygonSoup//所以要当成PolygonSoup读入std::vector<Point> points;std::vector<std::vector<std::size_t> > polygons;if (!CGAL::IO::read_polygon_soup(fp, points, polygons) || points.empty()){throw "Cannot open file ";}//转换成PolygonMeshCGAL::Polygon_mesh_processing::orient_polygon_soup(points, polygons);Polyhedron mesh;CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, polygons, mesh);return mesh;}void save(const Nef_polyhedron& nef, const std::string& outfp){Surface_mesh output;CGAL::convert_nef_polyhedron_to_polygon_mesh(nef, output);std::ofstream out;out.open(outfp);out << output;out.close();}void printTime(const QTime& time){int time_Diff = time.elapsed(); //返回从上次start()或restart()开始以来的时间差,单位ms//以下方法是将ms转为sfloat f = time_Diff / 1000.0;QString tr_timeDiff = QString("%1").arg(f); //float->QStringstd::cout << tr_timeDiff.toStdString() << "s" << std::endl;}int main(){//找到dirPath下的off,然后融合QString dirPath = R"(F:\00data\00work\GridToBrep20210819\data\wmodeler)";QString outFp = dirPath + "\\out.off";QTime time;time.start(); //开始计时,以ms为单位QDir dir(dirPath);dir.setNameFilters(QStringList() << "*.off");QStringList fileNames = dir.entryList();if (fileNames.size() == 0){return 1;}auto first = read_off(dirPath.toStdString(), fileNames[0].toStdString());Nef_polyhedron nef(first);int errorCnt = 0;std::cout << "个数\t时间(s)" << std::endl;//for (int i = 1; i < fileNames.size(); ++i)for (int i = 1; i < 1000; ++i){auto fn = fileNames[i];auto mesh = read_off(dirPath.toStdString(), fn.toStdString());try{Nef_polyhedron tmp(mesh);nef = nef + tmp;}catch (...){errorCnt++;}if (i % 100 == 1){save(nef, outFp.toStdString());cout << i << '\t';printTime(time);}}save(nef, outFp.toStdString());printTime(time);}
测试
N个长方体的融合
