1 // Copyright (c) 2004 INRIA Sophia-Antipolis (France).
2 // All rights reserved.
3 //
4 // This file is part of CGAL (www.cgal.org).
5 //
6 // $URL: https://github.com/CGAL/cgal/blob/v5.3/Mesh_2/include/CGAL/IO/File_poly.h $
7 // $Id: File_poly.h 625a335 2021-05-06T09:38:31+02:00 Sébastien Loriot
8 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
9 //
10 //
11 // Author(s) : Laurent RINEAU
12
13 #ifndef CGAL_FILE_POLY_H
14 #define CGAL_FILE_POLY_H
15
16 #include <CGAL/license/Mesh_2.h>
17 #include <CGAL/IO/OFF/File_scanner_OFF.h>
18
19 namespace CGAL {
20
21 namespace IO {
22
23 //the function that reads a Shewchuk Triangle .poly file
24 template <class CDT, class OutputIterator>
25 void
read_triangle_poly_file(CDT & t,std::istream & f,OutputIterator seeds)26 read_triangle_poly_file(CDT& t, std::istream &f,
27 OutputIterator seeds)
28 {
29 typedef typename CDT::Vertex_handle Vertex_handle;
30 typedef typename CDT::Point Point;
31
32 t.clear();
33
34 unsigned int number_of_points;
35 skip_comment_OFF(f);
36 f >> number_of_points;
37 skip_until_EOL(f);
38 skip_comment_OFF(f);
39
40 // read vertices
41 std::vector<Vertex_handle> vertices(number_of_points);
42 for(unsigned int i = 0; i < number_of_points; ++i)
43 {
44 unsigned int j;
45 double x, y;
46 f >> j >> iformat(x) >> iformat(y);
47 Point p(x, y);
48 skip_until_EOL(f); skip_comment_OFF(f);
49 vertices[--j] = t.insert(p);
50 }
51
52 // read segments
53 unsigned int number_of_segments;
54 f >> number_of_segments;
55 skip_until_EOL(f); skip_comment_OFF(f);
56 for(unsigned int k = 0; k < number_of_segments; ++k)
57 {
58 unsigned int l, v1, v2;
59 f >> l >> v1 >> v2;
60 skip_until_EOL(f); skip_comment_OFF(f);
61 t.insert_constraint(vertices[--v1], vertices[--v2]);
62 }
63
64 // read holes
65 unsigned int number_of_holes;
66 f >> number_of_holes;
67 for(unsigned int m = 0; m < number_of_holes; ++m)
68 {
69 unsigned int n;
70 Point p;
71 f >> n >> p;
72 skip_until_EOL(f); skip_comment_OFF(f);
73 *seeds++ = p;
74 }
75 }
76
77 template <class CDT>
78 inline
79 void
read_triangle_poly_file(CDT & t,std::istream & f)80 read_triangle_poly_file(CDT& t, std::istream &f)
81 {
82 read_triangle_poly_file(t, f, Emptyset_iterator());
83 }
84
85 //the function that write a Shewchuk Triangle .poly file
86 template <class CDT, typename InputIterator>
87 void
write_triangle_poly_file(const CDT & t,std::ostream & f,InputIterator begin,InputIterator end)88 write_triangle_poly_file(const CDT& t, std::ostream &f,
89 InputIterator begin, InputIterator end)
90 {
91 typedef typename CDT::Vertex_handle Vertex_handle;
92 typedef typename CDT::Finite_vertices_iterator
93 Finite_vertices_iterator;
94 typedef typename CDT::Finite_edges_iterator
95 Finite_edges_iterator;
96
97 std::map<Vertex_handle, unsigned int> index;
98
99 // write vertices
100 f << "# Shewchuk Triangle .poly file, produced by the CGAL::Mesh_2 package"
101 << std::endl
102 << "# Neither attributes nor boundary markers are used." << std::endl
103 << t.number_of_vertices() << " " << 2 << " "
104 << 0 << " " << 0 << std::endl;
105
106 f << std::endl;
107
108 unsigned int vertices_counter = 0;
109 for(Finite_vertices_iterator vit = t.finite_vertices_begin();
110 vit != t.finite_vertices_end();
111 ++vit)
112 {
113 f << ++vertices_counter << " " << vit->point() << std::endl;
114 index[vit] = vertices_counter;
115 }
116
117 f << std::endl;
118
119 // write constrained edges
120
121 int number_of_constrained_edges = 0;
122 for(Finite_edges_iterator it = t.finite_edges_begin();
123 it != t.finite_edges_end();
124 ++it)
125 if(it->first->is_constrained(it->second))
126 ++number_of_constrained_edges;
127
128 f << number_of_constrained_edges << " " << 0 << std::endl;
129 unsigned int edges_counter = 0;
130
131 for(Finite_edges_iterator eit = t.finite_edges_begin();
132 eit != t.finite_edges_end();
133 ++eit)
134 if(eit->first->is_constrained(eit->second))
135 f << ++edges_counter << " "
136 << index[eit->first->vertex(t.cw(eit->second))] << " "
137 << index[eit->first->vertex(t.ccw(eit->second))]
138 << std::endl;
139
140 f << std::endl;
141
142
143 // write seeds, assuming that the seeds unmarks faces
144 f << std::distance(begin, end) << std::endl;
145 unsigned int seeds_counter = 0;
146 for(InputIterator sit = begin;
147 sit!=end; ++sit)
148 f << ++seeds_counter << " " << *sit << std::endl;
149 }
150
151 //the same without holes.
152 template <class CDT>
153 void
write_triangle_poly_file(const CDT & t,std::ostream & f)154 write_triangle_poly_file(const CDT& t, std::ostream &f)
155 {
156 std::list<int> l;
157
158 write_triangle_poly_file(t, f, l.begin(), l.end());
159 }
160
161 } // namespace IO
162
163 #ifndef CGAL_NO_DEPRECATED_CODE
164 using IO::read_triangle_poly_file;
165 using IO::write_triangle_poly_file;
166 #endif
167
168 } // end namespace CGAL
169
170 #endif // CGAL_FILE_POLY_H
171