BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/IO/Arrangement_2_reader.h
Go to the documentation of this file.
00001 // Copyright (c) 2005-2007  Tel-Aviv University (Israel).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under
00005 // the terms of the Q Public License version 1.0.
00006 // See the file LICENSE.QPL distributed with CGAL.
00007 //
00008 // Licensees holding a valid commercial license may use this file in
00009 // accordance with the commercial license agreement provided with the software.
00010 //
00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 //
00014 // $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 $
00015 // $Id: Arrangement_2_reader.h 41108 2007-12-06 15:26:30Z efif $
00016 // 
00017 //
00018 // Author(s)     : Ron Wein           <wein@post.tau.ac.il>
00019 //                 (based on old version by Michal Meyerovitch and Ester Ezra)
00020 //
00021 #ifndef CGAL_IO_ARRANGEMENT_2_READER_H
00022 #define CGAL_IO_ARRANGEMENT_2_READER_H
00023 
00028 #include <CGAL/Arr_accessor.h>
00029 #include <CGAL/iterator.h>
00030 #include <CGAL/circulator.h>
00031 #include <algorithm>
00032 #include <iostream>
00033 
00034 CGAL_BEGIN_NAMESPACE
00035 
00039 template <class Arrangement_>
00040 class Arrangement_2_reader
00041 {
00042 public:
00043 
00044   typedef Arrangement_                                    Arrangement_2;
00045   typedef Arrangement_2_reader<Arrangement_2>             Self;
00046 
00047 protected:
00048  
00049   typedef typename Arrangement_2::Size                    Size;
00050   typedef typename Arrangement_2::Dcel                    Dcel;  
00051 
00052   typedef typename Arrangement_2::X_monotone_curve_2      X_monotone_curve_2;
00053   typedef typename Arrangement_2::Point_2                 Point_2;
00054 
00055   typedef typename Arrangement_2::Vertex_handle           Vertex_handle;
00056   typedef typename Arrangement_2::Halfedge_handle         Halfedge_handle;
00057   typedef typename Arrangement_2::Face_handle             Face_handle;
00058 
00059   typedef CGAL::Arr_accessor<Arrangement_2>               Arr_accessor;
00060   typedef typename Arr_accessor::Dcel_vertex              DVertex;
00061   typedef typename Arr_accessor::Dcel_halfedge            DHalfedge;
00062   typedef typename Arr_accessor::Dcel_face                DFace;
00063   typedef typename Arr_accessor::Dcel_outer_ccb           DOuter_ccb;
00064   typedef typename Arr_accessor::Dcel_inner_ccb           DInner_ccb;
00065   typedef typename Arr_accessor::Dcel_isolated_vertex     DIso_vert;
00066   
00067   // Data members:
00068   Arrangement_2&           m_arr;
00069   Arr_accessor             m_arr_access;
00070   Point_2                  m_point;
00071   std::vector<DVertex*>    m_vertices;
00072   X_monotone_curve_2       m_curve;
00073   std::vector<DHalfedge*>  m_halfedges;
00074 
00075 private:
00076 
00077   // Copy constructor and assignment operator - not supported.
00078   Arrangement_2_reader (const Self& );
00079   Self& operator= (const Self& );
00080 
00081 public:
00082 
00084   Arrangement_2_reader (Arrangement_2& arr) :
00085     m_arr (arr),
00086     m_arr_access (arr)
00087   {}
00088 
00090   virtual ~Arrangement_2_reader ()
00091   {}
00092 
00094   template <class Formatter>
00095   void operator()(Formatter& formatter)
00096   {
00097     // Clear the exisiting arrangement so it contains no DCEL features.
00098     m_arr_access.clear_all();
00099 
00100     // Read the arrangement dimensions.
00101     formatter.read_arrangement_begin();
00102 
00103     const Size  number_of_vertices = formatter.read_size("number_of_vertices");
00104     const Size  number_of_halfedges = 2*formatter.read_size("number_of_edges");
00105     const Size  number_of_faces = formatter.read_size("number_of_faces");
00106     Size        k;
00107     
00108     // Read the DCEL vertices and store them in the vertices vector.
00109     formatter.read_vertices_begin();
00110 
00111     m_vertices.resize (number_of_vertices);
00112     for (k = 0; k < number_of_vertices; k++)
00113       m_vertices[k] = _read_vertex (formatter);
00114 
00115     formatter.read_vertices_end();
00116 
00117     // Read the DCEL halfedges and store them in the halfedges vector.
00118     DHalfedge   *he = NULL;
00119 
00120     formatter.read_edges_begin();
00121 
00122     m_halfedges.resize (number_of_halfedges);
00123     for (k = 0; k < number_of_halfedges; k += 2)
00124     { 
00125       he = _read_edge (formatter);
00126       m_halfedges[k] = he;
00127       m_halfedges[k + 1] = he->opposite();
00128     }
00129     formatter.read_edges_end();
00130 
00131     // Read the DCEL faces.
00132     formatter.read_faces_begin();
00133     for (k = 0; k < number_of_faces; k++)
00134       _read_face (formatter);
00135     formatter.read_faces_end();
00136 
00137     formatter.read_arrangement_end();
00138 
00139     // Use the accessor an update the topology-traits properties with the
00140     // new DCEL we have just read.
00141     m_arr_access.dcel_updated();
00142 
00143     return;
00144   }
00145 
00146 protected:
00147 
00149   template <class Formatter>
00150   DVertex* _read_vertex (Formatter& formatter)
00151   {
00152     formatter.read_vertex_begin();
00153 
00154     // Read the boundary conditions.
00155     Arr_parameter_space   ps_x =
00156       Arr_parameter_space (formatter.read_vertex_index());
00157     Arr_parameter_space   ps_y =
00158       Arr_parameter_space (formatter.read_vertex_index());
00159     int             has_point = formatter.read_vertex_index();
00160     DVertex        *new_v;
00161 
00162     if (has_point)
00163     {
00164       // Read the point associated with the vertex.
00165       formatter.read_point (m_point);
00166 
00167       // Allocate a new DCEL vertex and associate it with this point.
00168       new_v = m_arr_access.new_vertex (&m_point, ps_x, ps_y);
00169 
00170       // Read any auxiliary data associated with the vertex.
00171       formatter.read_vertex_data (Vertex_handle (new_v));
00172     }
00173     else
00174     {
00175       // Allocate a vertex at infinity.
00176       new_v = m_arr_access.new_vertex (NULL, ps_x, ps_y);
00177     }
00178 
00179     formatter.read_vertex_end();
00180     return (new_v);
00181   }
00182  
00184   template <class Formatter>
00185   DHalfedge* _read_edge (Formatter& formatter)
00186   {
00187     formatter.read_edge_begin();
00188 
00189     // Read the indices of the end-vertices and the edge direction.
00190     int                 source_idx = formatter.read_vertex_index();
00191     int                 target_idx = formatter.read_vertex_index();
00192     int                 direction = formatter.read_vertex_index();
00193     int                 has_curve = formatter.read_vertex_index();
00194     DHalfedge          *new_he;
00195     DVertex            *src_v = m_vertices[source_idx];
00196     DVertex            *trg_v = m_vertices[target_idx];
00197 
00198     if (has_curve)
00199     {
00200       // Read the x-monotone curve associated with the edge. 
00201       formatter.read_x_monotone_curve (m_curve);
00202 
00203       // Allocate a pair of new DCEL halfegdes and associate them with the
00204       // x-monotone curve we read.
00205       new_he = m_arr_access.new_edge (&m_curve);
00206     }
00207     else
00208     {
00209       // Allocate a new fictitious edge.
00210       new_he = m_arr_access.new_edge (NULL);
00211     }
00212 
00213     // Set the cross pointers between the twin halfedges and the end vertices.
00214     trg_v->set_halfedge (new_he);
00215     new_he->set_vertex (trg_v);
00216     
00217     src_v->set_halfedge (new_he->opposite());
00218     new_he->opposite()->set_vertex (src_v);
00219    
00220     // Set the direction of the halfedges.
00221     if (direction == 0)
00222     {
00223       new_he->set_direction (ARR_LEFT_TO_RIGHT);
00224     }
00225     else
00226     {
00227       CGAL_assertion (direction == 1);
00228       new_he->set_direction (ARR_RIGHT_TO_LEFT);
00229     }
00230 
00231     // Read any auxiliary data associated with the halfedges.
00232     if (has_curve)
00233     {
00234       formatter.read_halfedge_data (Halfedge_handle (new_he));
00235       formatter.read_halfedge_data (Halfedge_handle ((new_he->opposite())));
00236     }
00237 
00238     formatter.read_edge_end();
00239     return (new_he);
00240   }
00241 
00243   template <class Formatter>
00244   void _read_face(Formatter& formatter)
00245   {
00246     formatter.read_face_begin();
00247 
00248     // Allocate a new face and determine whether it is unbounded and wether it
00249     // is valid (non-fictitious).
00250     DFace              *new_f = m_arr_access.new_face();
00251     const bool          is_unbounded = (formatter.read_vertex_index() != 0);
00252     const bool          is_valid = (formatter.read_vertex_index() != 0);
00253 
00254     new_f->set_unbounded (is_unbounded);
00255     new_f->set_fictitious (! is_valid);
00256 
00257     // Read the outer CCBs of the face.
00258     formatter.read_outer_ccbs_begin();
00259 
00260     DOuter_ccb  *new_occb;
00261     const Size   n_occbs = formatter.read_size ("number_of_outer_ccbs");
00262     DHalfedge   *he;
00263     Size         n, k;
00264 
00265     for (k = 0; k < n_occbs; k++)
00266     {
00267       // Allocate a new outer CCB record and set its incident face.
00268       new_occb = m_arr_access.new_outer_ccb();
00269       new_occb->set_face (new_f);
00270 
00271       // Read the current outer CCB.
00272       n = formatter.read_size ("halfedges_on_outer_ccb");
00273       he = _read_ccb (formatter, n, new_occb, NULL);
00274       new_f->add_outer_ccb (new_occb, he);
00275     }
00276     formatter.read_outer_ccbs_end();
00277 
00278     // Read the inner CCBs of the face.
00279     formatter.read_inner_ccbs_begin();
00280 
00281     DInner_ccb  *new_iccb;
00282     const Size   n_iccbs = formatter.read_size ("number_of_inner_ccbs");
00283 
00284     for (k = 0; k < n_iccbs; k++)
00285     {
00286       // Allocate a new inner CCB record and set its incident face.
00287       new_iccb = m_arr_access.new_inner_ccb();
00288       new_iccb->set_face (new_f);
00289 
00290       // Read the current inner CCB.
00291       n = formatter.read_size ("halfedges_on_inner_ccb");
00292       he = _read_ccb (formatter, n, NULL, new_iccb);
00293       new_f->add_inner_ccb (new_iccb, he);
00294     }
00295     formatter.read_inner_ccbs_end();
00296 
00297     // Read the isolated vertices inside the face.
00298     formatter.read_isolated_vertices_begin();
00299 
00300     DIso_vert   *new_iso_vert;
00301     Size         n_isolated_vertices = 
00302                           formatter.read_size ("number_of_isolated_vertices");
00303     std::size_t  v_idx;
00304     DVertex*     iso_v;
00305 
00306     for (k = 0; k < n_isolated_vertices; k++)
00307     {
00308       // Allocate a new isolated vertex record and set its incident face.
00309       new_iso_vert = m_arr_access.new_isolated_vertex();
00310       new_iso_vert->set_face (new_f);
00311 
00312       // Read the current isolated vertex.
00313       v_idx = formatter.read_vertex_index ();
00314       iso_v = m_vertices[v_idx];
00315       iso_v->set_isolated_vertex (new_iso_vert);
00316       new_f->add_isolated_vertex (new_iso_vert, iso_v);
00317     }
00318     formatter.read_isolated_vertices_end();
00319 
00320     // Read any auxiliary data associated with the face.
00321     if (is_valid)
00322       formatter.read_face_data (Face_handle (new_f));
00323 
00324     formatter.read_face_end();
00325 
00326     return;
00327   }
00328 
00338   template <class Formatter>
00339   DHalfedge* _read_ccb (Formatter& formatter, 
00340                         Size boundary_size,
00341                         DOuter_ccb *p_outer,
00342                         DInner_ccb *p_inner)
00343   {
00344     CGAL_assertion ((p_outer != NULL && p_inner == NULL) ||
00345                     (p_outer == NULL && p_inner != NULL));
00346 
00347     formatter.read_ccb_halfedges_begin();
00348  
00349     // Find the first halfedge, and set its CCB.
00350     std::size_t   first_idx = formatter.read_halfedge_index();
00351     DHalfedge    *first_he = m_halfedges [first_idx];
00352 
00353     if (p_outer != NULL)
00354       first_he->set_outer_ccb (p_outer);
00355     else
00356       first_he->set_inner_ccb (p_inner);
00357 
00358     // Read the rest of the halfedge along the boundary.
00359     std::size_t   curr_idx;
00360     DHalfedge    *prev_he = first_he;    
00361     DHalfedge    *curr_he;
00362     Size          k;
00363 
00364     for (k = 1; k < boundary_size; k++)
00365     {
00366       curr_idx = formatter.read_halfedge_index();
00367       curr_he = m_halfedges[curr_idx];
00368 
00369       // Connect the previous halfedge and the current one.
00370       prev_he->set_next (curr_he);
00371 
00372       // Set the CCB.
00373       if (p_outer != NULL)
00374         curr_he->set_outer_ccb (p_outer);
00375       else
00376         curr_he->set_inner_ccb (p_inner);
00377 
00378       prev_he = curr_he;
00379     }
00380 
00381     // Close the circular list be connecting the first and the last halfedges.
00382     prev_he->set_next (first_he);
00383 
00384     formatter.read_ccb_halfedges_end();
00385 
00386     // Return the first halfedge.
00387     return (first_he);
00388   }
00389 
00390 };
00391 
00392 CGAL_END_NAMESPACE
00393 
00394 #endif // CGAL_IO_ARRANGEMENT_2_READER_H 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines