官方文档:CGAL 5.4支持的文件格式
CGAL提供一个函数,用来读写模型文件。支持格式:obj off ply stl ts(gocad) vtp(vtk)
#include<iostream>#include<string>#include<algorithm>//Polygon Mesh#include <CGAL/Surface_mesh/Surface_mesh.h>#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>typedef CGAL::Exact_predicates_inexact_constructions_kernel K;typedef K::Point_3 Point;typedef CGAL::Surface_mesh<Point> Surface_mesh;/*支持obj off ply stl ts(gocad) vtp(vtk)*注意*1. 数据需是一个二维流形(可能有边界)*2. 如果数据不是一个二维流形,可尝试使用CGAL::Polygon_mesh_processing::IO::read_polygon_mesh()*/#define CGAL_USE_VTK#include <CGAL/boost/graph/IO/polygon_mesh_io.h>int main(){std::string in_path = R"(C:\Users\2107\Desktop\RiverModeling.ts)";std::string out_path = R"(C:\Users\2107\Desktop\RiverModeling.obj)";Surface_mesh mesh;//内部会按照文件格式分流到具体的读写函数中if(!CGAL::IO::read_polygon_mesh(in_path, mesh)){return 0;}//内部会按照文件格式分流到具体的读写函数中if(!CGAL::IO::write_polygon_mesh(in_path, mesh)){std::cout << "Save Failed" << std::endl;return 0;}return 0;}
OFF格式
读入
(1)直接用>>读入
#include <CGAL/IO/OFF_reader.h>const char* filename = (argc > 1) ? argv[1] : "data/blobby.off";std::ifstream input(filename);Mesh mesh;if(!input || !(input >> mesh) || num_vertices(mesh) == 0) //直接使用>>{std::cerr << filename << " is not a valid off file.\n";return EXIT_FAILURE;}
写出
(1)直接用<<写出
#include <fstream>std::ofstream output("union.off");output.precision(17);output << mesh2; //重载了<<符号,<<符号就是输出off格式
(2)调用方法写出
#include <CGAL/IO/File_writer_OFF.h>void print_OFF(ostream& out) {CGAL::File_header_OFF header( binary, noc, skel, verbose);CGAL::File_writer_OFF writer( header);writer.write_header( out, points.size(), 0, facets.size());while( ! points.empty()) {writer.write_vertex( points.front().x(),points.front().y(),points.front().z());points.pop_front();}writer.write_facet_header();while( ! facets.empty()) {Facet& facet = facets.front();writer.write_facet_begin( facet.size());while( ! facet.empty()) {writer.write_facet_vertex_index( facet.front());facet.pop_front();}writer.write_facet_end();facets.pop_front();}writer.write_footer();}
ply
#include<CGAL/io/PLY/PLY_writer.h>CGAL::IO::write_PLY("tmp.ply", sm);
obj
#include<CGAL/io/obj/File_writer_wavefront.h>
读写代码
IO代码:
示例:
examples\Polyhedron_IO\iv2off.cpp
