|
| 1 | +#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> |
| 2 | +#include <CGAL/IO/File_binary_mesh_3.h> |
| 3 | +#include <CGAL/IO/output_to_vtu.h> |
| 4 | +#include <CGAL/Mesh_complex_3_in_triangulation_3.h> |
| 5 | +#include <CGAL/Mesh_criteria_3.h> |
| 6 | +#include <CGAL/Mesh_triangulation_3.h> |
| 7 | +#include <CGAL/Polyhedral_mesh_domain_with_features_3.h> |
| 8 | +#include <CGAL/Surface_mesh.h> |
| 9 | +#include <CGAL/boost/graph/helpers.h> |
| 10 | +#include <CGAL/make_mesh_3.h> |
| 11 | +#include <fstream> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +// Domain |
| 15 | +using K = CGAL::Exact_predicates_inexact_constructions_kernel; |
| 16 | +using Polyhedron = CGAL::Surface_mesh<K::Point_3>; |
| 17 | +using Mesh_domain = CGAL::Polyhedral_mesh_domain_with_features_3<K, Polyhedron>; |
| 18 | + |
| 19 | +// Triangulation |
| 20 | +using Tr = CGAL::Mesh_triangulation_3<Mesh_domain, CGAL::Default, |
| 21 | + CGAL::Parallel_if_available_tag>::type; |
| 22 | +using C3t3 = CGAL::Mesh_complex_3_in_triangulation_3<Tr>; |
| 23 | + |
| 24 | +// Criteria |
| 25 | +using Mesh_criteria = CGAL::Mesh_criteria_3<Tr>; |
| 26 | + |
| 27 | +namespace params = CGAL::parameters; |
| 28 | + |
| 29 | +void check_stream(const std::ios& stream, |
| 30 | + const std::string& filename, |
| 31 | + const std::string& operation, |
| 32 | + bool ok = true) { |
| 33 | + if(!stream || !ok) { |
| 34 | + std::cerr << "Stream error after " << operation << " file: " << filename << std::endl; |
| 35 | + std::cerr << "Stream state: "; |
| 36 | + if(stream.rdstate() == std::ios::goodbit) { |
| 37 | + std::cerr << "no error"; |
| 38 | + } else { |
| 39 | + if(stream.rdstate() & std::ios::eofbit) { |
| 40 | + std::cerr << "eofbit "; |
| 41 | + } |
| 42 | + if(stream.rdstate() & std::ios::failbit) { |
| 43 | + std::cerr << "failbit "; |
| 44 | + } |
| 45 | + if(stream.rdstate() & std::ios::badbit) { |
| 46 | + std::cerr << "badbit "; |
| 47 | + } |
| 48 | + } |
| 49 | + std::cerr << std::endl; |
| 50 | + std::exit(EXIT_FAILURE); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +int main(int argc, char* argv[]) { |
| 55 | + const std::string fname = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/cube.off"); |
| 56 | + |
| 57 | + // Create input polyhedron |
| 58 | + Polyhedron polyhedron; |
| 59 | + std::ifstream input(fname); |
| 60 | + check_stream(input, fname, "opening"); |
| 61 | + input >> polyhedron; |
| 62 | + check_stream(input, fname, "reading polyhedron from"); |
| 63 | + |
| 64 | + // Create domain |
| 65 | + Mesh_domain domain(polyhedron); |
| 66 | + domain.detect_features(); |
| 67 | + |
| 68 | + // Mesh criteria (no cell_size set) |
| 69 | + Mesh_criteria criteria(params::facet_angle(25).facet_size(0.15).facet_distance(0.05).cell_radius_edge_ratio(3)); |
| 70 | + |
| 71 | + // Mesh generation |
| 72 | + const C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain, criteria, params::no_perturb().no_exude()); |
| 73 | + |
| 74 | + const auto nb_vertices = c3t3.triangulation().number_of_vertices(); |
| 75 | + const auto nb_far_vertices = c3t3.number_of_far_points(); |
| 76 | + const auto nb_cells = c3t3.number_of_cells(); |
| 77 | + |
| 78 | + std::cout << "Created a c3t3 with " << c3t3.triangulation().number_of_vertices() << " vertices, and " |
| 79 | + << c3t3.number_of_cells() << " cells" << std::endl; |
| 80 | + |
| 81 | + // Output |
| 82 | + { |
| 83 | + const std::string filename = "ascii.vtu"; |
| 84 | + std::ofstream out(filename); |
| 85 | + check_stream(out, filename, "opening"); |
| 86 | + CGAL::IO::output_to_vtu(out, c3t3, CGAL::IO::ASCII); |
| 87 | + check_stream(out, filename, "writing (ASCII)"); |
| 88 | + } |
| 89 | + |
| 90 | + { |
| 91 | + const std::string filename = "binary.vtu"; |
| 92 | + std::ofstream out(filename, std::ios::binary); |
| 93 | + check_stream(out, filename, "opening"); |
| 94 | + CGAL::IO::output_to_vtu(out, c3t3, CGAL::IO::BINARY); |
| 95 | + check_stream(out, filename, "writing (BINARY)"); |
| 96 | + } |
| 97 | + |
| 98 | + const std::string filename = "mesh.binary.cgal"; |
| 99 | + { |
| 100 | + std::ofstream out(filename, std::ios::binary); |
| 101 | + check_stream(out, filename, "opening"); |
| 102 | + bool ok = CGAL::IO::save_binary_file(out, c3t3); |
| 103 | + check_stream(out, filename, "writing (binary)", ok); |
| 104 | + } |
| 105 | + |
| 106 | + // Input |
| 107 | + C3t3 bis; |
| 108 | + { |
| 109 | + std::ifstream in(filename, std::ios::binary); |
| 110 | + check_stream(in, filename, "opening (binary)"); |
| 111 | + bool ok = CGAL::IO::load_binary_file(in, bis); |
| 112 | + check_stream(in, filename, "reading binary file", ok); |
| 113 | + } |
| 114 | + |
| 115 | + { |
| 116 | + const std::string bis_filename = "bis.vtu"; |
| 117 | + std::ofstream bis_os(bis_filename); |
| 118 | + check_stream(bis_os, bis_filename, "opening (bis.vtu)"); |
| 119 | + CGAL::IO::output_to_vtu(bis_os, bis, CGAL::IO::ASCII); |
| 120 | + check_stream(bis_os, bis_filename, "writing (ASCII)"); |
| 121 | + } |
| 122 | + |
| 123 | + assert(bis.number_of_cells() == nb_cells); |
| 124 | + assert(bis.triangulation().number_of_vertices() == nb_vertices); |
| 125 | + assert(bis.number_of_far_points() == nb_far_vertices); |
| 126 | + |
| 127 | + std::cout << "Mesh generation and output completed successfully." << std::endl; |
| 128 | + |
| 129 | + return EXIT_SUCCESS; |
| 130 | +} |
0 commit comments