官方文档:CGAL 5.4支持的文件格式

CGAL提供一个函数,用来读写模型文件。支持格式:obj off ply stl ts(gocad) vtp(vtk)

  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. //Polygon Mesh
  5. #include <CGAL/Surface_mesh/Surface_mesh.h>
  6. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  7. typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
  8. typedef K::Point_3 Point;
  9. typedef CGAL::Surface_mesh<Point> Surface_mesh;
  10. /*支持obj off ply stl ts(gocad) vtp(vtk)
  11. *注意
  12. *1. 数据需是一个二维流形(可能有边界)
  13. *2. 如果数据不是一个二维流形,可尝试使用CGAL::Polygon_mesh_processing::IO::read_polygon_mesh()
  14. */
  15. #define CGAL_USE_VTK
  16. #include <CGAL/boost/graph/IO/polygon_mesh_io.h>
  17. int main()
  18. {
  19. std::string in_path = R"(C:\Users\2107\Desktop\RiverModeling.ts)";
  20. std::string out_path = R"(C:\Users\2107\Desktop\RiverModeling.obj)";
  21. Surface_mesh mesh;
  22. //内部会按照文件格式分流到具体的读写函数中
  23. if(!CGAL::IO::read_polygon_mesh(in_path, mesh))
  24. {
  25. return 0;
  26. }
  27. //内部会按照文件格式分流到具体的读写函数中
  28. if(!CGAL::IO::write_polygon_mesh(in_path, mesh))
  29. {
  30. std::cout << "Save Failed" << std::endl;
  31. return 0;
  32. }
  33. return 0;
  34. }

OFF格式

读入

(1)直接用>>读入

  1. #include <CGAL/IO/OFF_reader.h>
  2. const char* filename = (argc > 1) ? argv[1] : "data/blobby.off";
  3. std::ifstream input(filename);
  4. Mesh mesh;
  5. if(!input || !(input >> mesh) || num_vertices(mesh) == 0) //直接使用>>
  6. {
  7. std::cerr << filename << " is not a valid off file.\n";
  8. return EXIT_FAILURE;
  9. }

写出

(1)直接用<<写出

  1. #include <fstream>
  2. std::ofstream output("union.off");
  3. output.precision(17);
  4. output << mesh2; //重载了<<符号,<<符号就是输出off格式

(2)调用方法写出

  1. #include <CGAL/IO/File_writer_OFF.h>
  2. void print_OFF(ostream& out) {
  3. CGAL::File_header_OFF header( binary, noc, skel, verbose);
  4. CGAL::File_writer_OFF writer( header);
  5. writer.write_header( out, points.size(), 0, facets.size());
  6. while( ! points.empty()) {
  7. writer.write_vertex( points.front().x(),
  8. points.front().y(),
  9. points.front().z());
  10. points.pop_front();
  11. }
  12. writer.write_facet_header();
  13. while( ! facets.empty()) {
  14. Facet& facet = facets.front();
  15. writer.write_facet_begin( facet.size());
  16. while( ! facet.empty()) {
  17. writer.write_facet_vertex_index( facet.front());
  18. facet.pop_front();
  19. }
  20. writer.write_facet_end();
  21. facets.pop_front();
  22. }
  23. writer.write_footer();
  24. }

ply

  1. #include<CGAL/io/PLY/PLY_writer.h>
  2. CGAL::IO::write_PLY("tmp.ply", sm);

obj

  1. #include<CGAL/io/obj/File_writer_wavefront.h>

读写代码

IO代码:
image.png
示例:

  1. examples\Polyhedron_IO\iv2off.cpp