File: Arrangement_2_reader.h

package info (click to toggle)
cgal 3.6.1-2
  • links: PTS
  • area: non-free
  • in suites: squeeze
  • size: 62,184 kB
  • ctags: 95,782
  • sloc: cpp: 453,758; ansic: 96,821; sh: 226; makefile: 120; xml: 2
file content (394 lines) | stat: -rw-r--r-- 12,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (c) 2005-2007  Tel-Aviv University (Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you may redistribute it under
// the terms of the Q Public License version 1.0.
// See the file LICENSE.QPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Arrangement_on_surface_2/include/CGAL/IO/Arrangement_2_reader.h $
// $Id: Arrangement_2_reader.h 41108 2007-12-06 15:26:30Z efif $
// 
//
// Author(s)     : Ron Wein           <wein@post.tau.ac.il>
//                 (based on old version by Michal Meyerovitch and Ester Ezra)
//
#ifndef CGAL_IO_ARRANGEMENT_2_READER_H
#define CGAL_IO_ARRANGEMENT_2_READER_H

/*! \file
 * The header file for the Arrangement_2_reader<Arrangement> class.
 */

#include <CGAL/Arr_accessor.h>
#include <CGAL/iterator.h>
#include <CGAL/circulator.h>
#include <algorithm>
#include <iostream>

CGAL_BEGIN_NAMESPACE

/*! \class
 * An auxiliary class for reading an arrangement from an input stream.
 */
template <class Arrangement_>
class Arrangement_2_reader
{
public:

  typedef Arrangement_                                    Arrangement_2;
  typedef Arrangement_2_reader<Arrangement_2>             Self;

protected:
 
  typedef typename Arrangement_2::Size                    Size;
  typedef typename Arrangement_2::Dcel                    Dcel;  

  typedef typename Arrangement_2::X_monotone_curve_2      X_monotone_curve_2;
  typedef typename Arrangement_2::Point_2                 Point_2;

  typedef typename Arrangement_2::Vertex_handle           Vertex_handle;
  typedef typename Arrangement_2::Halfedge_handle         Halfedge_handle;
  typedef typename Arrangement_2::Face_handle             Face_handle;

  typedef CGAL::Arr_accessor<Arrangement_2>               Arr_accessor;
  typedef typename Arr_accessor::Dcel_vertex              DVertex;
  typedef typename Arr_accessor::Dcel_halfedge            DHalfedge;
  typedef typename Arr_accessor::Dcel_face                DFace;
  typedef typename Arr_accessor::Dcel_outer_ccb           DOuter_ccb;
  typedef typename Arr_accessor::Dcel_inner_ccb           DInner_ccb;
  typedef typename Arr_accessor::Dcel_isolated_vertex     DIso_vert;
  
  // Data members:
  Arrangement_2&           m_arr;
  Arr_accessor             m_arr_access;
  Point_2                  m_point;
  std::vector<DVertex*>    m_vertices;
  X_monotone_curve_2       m_curve;
  std::vector<DHalfedge*>  m_halfedges;

private:

  // Copy constructor and assignment operator - not supported.
  Arrangement_2_reader (const Self& );
  Self& operator= (const Self& );

public:

  /*! Constructor. */
  Arrangement_2_reader (Arrangement_2& arr) :
    m_arr (arr),
    m_arr_access (arr)
  {}

  /*! Destructor. */
  virtual ~Arrangement_2_reader ()
  {}

  /*! Read the arrangement. */
  template <class Formatter>
  void operator()(Formatter& formatter)
  {
    // Clear the exisiting arrangement so it contains no DCEL features.
    m_arr_access.clear_all();

    // Read the arrangement dimensions.
    formatter.read_arrangement_begin();

    const Size  number_of_vertices = formatter.read_size("number_of_vertices");
    const Size  number_of_halfedges = 2*formatter.read_size("number_of_edges");
    const Size  number_of_faces = formatter.read_size("number_of_faces");
    Size        k;
    
    // Read the DCEL vertices and store them in the vertices vector.
    formatter.read_vertices_begin();

    m_vertices.resize (number_of_vertices);
    for (k = 0; k < number_of_vertices; k++)
      m_vertices[k] = _read_vertex (formatter);

    formatter.read_vertices_end();

    // Read the DCEL halfedges and store them in the halfedges vector.
    DHalfedge   *he = NULL;

    formatter.read_edges_begin();

    m_halfedges.resize (number_of_halfedges);
    for (k = 0; k < number_of_halfedges; k += 2)
    { 
      he = _read_edge (formatter);
      m_halfedges[k] = he;
      m_halfedges[k + 1] = he->opposite();
    }
    formatter.read_edges_end();

    // Read the DCEL faces.
    formatter.read_faces_begin();
    for (k = 0; k < number_of_faces; k++)
      _read_face (formatter);
    formatter.read_faces_end();

    formatter.read_arrangement_end();

    // Use the accessor an update the topology-traits properties with the
    // new DCEL we have just read.
    m_arr_access.dcel_updated();

    return;
  }

protected:

  /*! Read a DCEL vertex. */
  template <class Formatter>
  DVertex* _read_vertex (Formatter& formatter)
  {
    formatter.read_vertex_begin();

    // Read the boundary conditions.
    Arr_parameter_space   ps_x =
      Arr_parameter_space (formatter.read_vertex_index());
    Arr_parameter_space   ps_y =
      Arr_parameter_space (formatter.read_vertex_index());
    int             has_point = formatter.read_vertex_index();
    DVertex        *new_v;

    if (has_point)
    {
      // Read the point associated with the vertex.
      formatter.read_point (m_point);

      // Allocate a new DCEL vertex and associate it with this point.
      new_v = m_arr_access.new_vertex (&m_point, ps_x, ps_y);

      // Read any auxiliary data associated with the vertex.
      formatter.read_vertex_data (Vertex_handle (new_v));
    }
    else
    {
      // Allocate a vertex at infinity.
      new_v = m_arr_access.new_vertex (NULL, ps_x, ps_y);
    }

    formatter.read_vertex_end();
    return (new_v);
  }
 
  /*! Read a DCEL edge (a pair of twin halfedges). */
  template <class Formatter>
  DHalfedge* _read_edge (Formatter& formatter)
  {
    formatter.read_edge_begin();

    // Read the indices of the end-vertices and the edge direction.
    int                 source_idx = formatter.read_vertex_index();
    int                 target_idx = formatter.read_vertex_index();
    int                 direction = formatter.read_vertex_index();
    int                 has_curve = formatter.read_vertex_index();
    DHalfedge          *new_he;
    DVertex            *src_v = m_vertices[source_idx];
    DVertex            *trg_v = m_vertices[target_idx];

    if (has_curve)
    {
      // Read the x-monotone curve associated with the edge. 
      formatter.read_x_monotone_curve (m_curve);

      // Allocate a pair of new DCEL halfegdes and associate them with the
      // x-monotone curve we read.
      new_he = m_arr_access.new_edge (&m_curve);
    }
    else
    {
      // Allocate a new fictitious edge.
      new_he = m_arr_access.new_edge (NULL);
    }

    // Set the cross pointers between the twin halfedges and the end vertices.
    trg_v->set_halfedge (new_he);
    new_he->set_vertex (trg_v);
    
    src_v->set_halfedge (new_he->opposite());
    new_he->opposite()->set_vertex (src_v);
   
    // Set the direction of the halfedges.
    if (direction == 0)
    {
      new_he->set_direction (ARR_LEFT_TO_RIGHT);
    }
    else
    {
      CGAL_assertion (direction == 1);
      new_he->set_direction (ARR_RIGHT_TO_LEFT);
    }

    // Read any auxiliary data associated with the halfedges.
    if (has_curve)
    {
      formatter.read_halfedge_data (Halfedge_handle (new_he));
      formatter.read_halfedge_data (Halfedge_handle ((new_he->opposite())));
    }

    formatter.read_edge_end();
    return (new_he);
  }

  /*! Read a DCEL face. */
  template <class Formatter>
  void _read_face(Formatter& formatter)
  {
    formatter.read_face_begin();

    // Allocate a new face and determine whether it is unbounded and wether it
    // is valid (non-fictitious).
    DFace              *new_f = m_arr_access.new_face();
    const bool          is_unbounded = (formatter.read_vertex_index() != 0);
    const bool          is_valid = (formatter.read_vertex_index() != 0);

    new_f->set_unbounded (is_unbounded);
    new_f->set_fictitious (! is_valid);

    // Read the outer CCBs of the face.
    formatter.read_outer_ccbs_begin();

    DOuter_ccb  *new_occb;
    const Size   n_occbs = formatter.read_size ("number_of_outer_ccbs");
    DHalfedge   *he;
    Size         n, k;

    for (k = 0; k < n_occbs; k++)
    {
      // Allocate a new outer CCB record and set its incident face.
      new_occb = m_arr_access.new_outer_ccb();
      new_occb->set_face (new_f);

      // Read the current outer CCB.
      n = formatter.read_size ("halfedges_on_outer_ccb");
      he = _read_ccb (formatter, n, new_occb, NULL);
      new_f->add_outer_ccb (new_occb, he);
    }
    formatter.read_outer_ccbs_end();

    // Read the inner CCBs of the face.
    formatter.read_inner_ccbs_begin();

    DInner_ccb  *new_iccb;
    const Size   n_iccbs = formatter.read_size ("number_of_inner_ccbs");

    for (k = 0; k < n_iccbs; k++)
    {
      // Allocate a new inner CCB record and set its incident face.
      new_iccb = m_arr_access.new_inner_ccb();
      new_iccb->set_face (new_f);

      // Read the current inner CCB.
      n = formatter.read_size ("halfedges_on_inner_ccb");
      he = _read_ccb (formatter, n, NULL, new_iccb);
      new_f->add_inner_ccb (new_iccb, he);
    }
    formatter.read_inner_ccbs_end();

    // Read the isolated vertices inside the face.
    formatter.read_isolated_vertices_begin();

    DIso_vert   *new_iso_vert;
    Size         n_isolated_vertices = 
                          formatter.read_size ("number_of_isolated_vertices");
    std::size_t  v_idx;
    DVertex*     iso_v;

    for (k = 0; k < n_isolated_vertices; k++)
    {
      // Allocate a new isolated vertex record and set its incident face.
      new_iso_vert = m_arr_access.new_isolated_vertex();
      new_iso_vert->set_face (new_f);

      // Read the current isolated vertex.
      v_idx = formatter.read_vertex_index ();
      iso_v = m_vertices[v_idx];
      iso_v->set_isolated_vertex (new_iso_vert);
      new_f->add_isolated_vertex (new_iso_vert, iso_v);
    }
    formatter.read_isolated_vertices_end();

    // Read any auxiliary data associated with the face.
    if (is_valid)
      formatter.read_face_data (Face_handle (new_f));

    formatter.read_face_end();

    return;
  }

  /*!
   * Read a circular boundary of a conncted component.
   * \param formatter The formatter.
   * \param boundary_size The number of halfedges along the boundary.
   * \param p_outer The outer CCB.
   * \param p_inner The inner CCB.
   * \pre p_outer is valid and p_inner is NULL, or vice versa.
   * \return A pointer to the first halfedge read.
   */
  template <class Formatter>
  DHalfedge* _read_ccb (Formatter& formatter, 
                        Size boundary_size,
                        DOuter_ccb *p_outer,
                        DInner_ccb *p_inner)
  {
    CGAL_assertion ((p_outer != NULL && p_inner == NULL) ||
                    (p_outer == NULL && p_inner != NULL));

    formatter.read_ccb_halfedges_begin();
 
    // Find the first halfedge, and set its CCB.
    std::size_t   first_idx = formatter.read_halfedge_index();
    DHalfedge    *first_he = m_halfedges [first_idx];

    if (p_outer != NULL)
      first_he->set_outer_ccb (p_outer);
    else
      first_he->set_inner_ccb (p_inner);

    // Read the rest of the halfedge along the boundary.
    std::size_t   curr_idx;
    DHalfedge    *prev_he = first_he;    
    DHalfedge    *curr_he;
    Size          k;

    for (k = 1; k < boundary_size; k++)
    {
      curr_idx = formatter.read_halfedge_index();
      curr_he = m_halfedges[curr_idx];

      // Connect the previous halfedge and the current one.
      prev_he->set_next (curr_he);

      // Set the CCB.
      if (p_outer != NULL)
        curr_he->set_outer_ccb (p_outer);
      else
        curr_he->set_inner_ccb (p_inner);

      prev_he = curr_he;
    }

    // Close the circular list be connecting the first and the last halfedges.
    prev_he->set_next (first_he);

    formatter.read_ccb_halfedges_end();

    // Return the first halfedge.
    return (first_he);
  }

};

CGAL_END_NAMESPACE

#endif // CGAL_IO_ARRANGEMENT_2_READER_H