BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/IO/Arrangement_2_writer.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_writer.h $
00015 // $Id: Arrangement_2_writer.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_WRITER_H
00022 #define CGAL_IO_ARRANGEMENT_2_WRITER_H
00023 
00028 #include <CGAL/Arr_accessor.h>
00029 #include <map>
00030 
00031 CGAL_BEGIN_NAMESPACE
00032 
00036 template <class Arrangement_>
00037 class Arrangement_2_writer
00038 {
00039 public:
00040 
00041   typedef Arrangement_                                    Arrangement_2;
00042   typedef Arrangement_2_writer<Arrangement_2>             Self;
00043 
00044 protected:
00045 
00046   typedef typename Arrangement_2::Size                    Size;
00047   typedef typename Arrangement_2::Dcel                    Dcel;
00048 
00049   typedef typename Arrangement_2::Vertex_const_handle   Vertex_const_handle;
00050   typedef typename Arrangement_2::Halfedge_const_handle Halfedge_const_handle;
00051   typedef typename Arrangement_2::Face_const_handle     Face_const_handle;
00052 
00053   typedef CGAL::Arr_accessor<Arrangement_2>             Arr_accessor;
00054   typedef typename Arr_accessor::Dcel_vertex_iterator   Vertex_const_iterator;
00055   typedef typename Arr_accessor::Dcel_edge_iterator     Edge_const_iterator;
00056   typedef typename Arr_accessor::Dcel_face_iterator     Face_const_iterator;
00057 
00058   typedef typename Arr_accessor::Dcel_outer_ccb_iterator
00059                                                    Outer_ccb_iterator;
00060   typedef typename Arr_accessor::Dcel_inner_ccb_iterator
00061                                                    Inner_ccb_iterator;
00062   typedef typename Arr_accessor::Dcel_iso_vertex_iterator
00063                                                    Isolated_vertex_iterator;
00064 
00065   typedef typename Arr_accessor::Dcel_vertex            DVertex;
00066   typedef typename Arr_accessor::Dcel_halfedge          DHalfedge;
00067   typedef typename Arr_accessor::Dcel_face              DFace;
00068   typedef std::map<const DVertex*, int>                 Vertex_index_map;
00069   typedef std::map<const DHalfedge*, int>               Halfedge_index_map;
00070 
00071   // Data memebrs:
00072   const Arrangement_2&   m_arr;
00073   const Dcel            *m_dcel;
00074   int                    m_curr_v;
00075   Vertex_index_map       m_v_index;
00076   int                    m_curr_he;
00077   Halfedge_index_map     m_he_index;
00078 
00079 private:
00080 
00081   // Copy constructor and assignment operator - not supported.
00082   Arrangement_2_writer (const Self& );
00083   Self& operator= (const Self& );
00084 
00085 public:
00086 
00088   Arrangement_2_writer (const Arrangement_2& arr) :
00089     m_arr (arr),
00090     m_dcel (NULL),
00091     m_curr_v (0),
00092     m_curr_he (0)
00093   {
00094     const Arr_accessor     arr_access (const_cast<Arrangement_2&>(arr));
00095     m_dcel = &(arr_access.dcel());
00096   }
00097 
00099   virtual ~Arrangement_2_writer()
00100   {}
00101 
00103   template <class Formatter>
00104   void operator() (Formatter& formatter)
00105   {
00106     formatter.write_arrangement_begin();
00107     formatter.write_size ("number_of_vertices",
00108                           m_dcel->size_of_vertices());
00109     formatter.write_size ("number_of_edges",
00110                           m_dcel->size_of_halfedges() / 2);
00111     formatter.write_size ("number_of_faces",
00112                           m_dcel->size_of_faces());
00113 
00114     // Reset indices.
00115     m_curr_v = 0;
00116     m_curr_he = 0;
00117 
00118     // Write the vertices.
00119     formatter.write_vertices_begin();
00120     Vertex_const_iterator  vit;
00121     for (vit = m_dcel->vertices_begin(); vit != m_dcel->vertices_end(); ++vit)
00122     {
00123       _write_vertex (formatter, vit);
00124     }
00125     formatter.write_vertices_end();
00126 
00127     // Write the edges.
00128     formatter.write_edges_begin();
00129     Edge_const_iterator    eit;
00130     for (eit = m_dcel->edges_begin(); eit != m_dcel->edges_end(); ++eit)
00131     {
00132       _write_edge (formatter, eit);
00133     }
00134     formatter.write_edges_end();
00135 
00136     // Write the faces (the fictitious face first).
00137     formatter.write_faces_begin();
00138 
00139     Face_const_iterator    fit;
00140     for (fit = m_dcel->faces_begin(); fit != m_dcel->faces_end(); ++fit)
00141       _write_face(formatter, fit);
00142     formatter.write_faces_end();
00143 
00144     formatter.write_arrangement_end();
00145   }
00146 
00147 protected:
00148 
00150   template <class Formatter>
00151   void _write_vertex (Formatter& formatter, Vertex_const_iterator vit)
00152   {
00153     // Map the current vertex to its index.
00154     const DVertex   *v = &(*vit);
00155 
00156     m_v_index[v] = m_curr_v;
00157     ++m_curr_v;
00158 
00159     // Write the vertex.
00160     formatter.write_vertex_begin();
00161     formatter.write_vertex_index (static_cast<int> (v->parameter_space_in_x()));
00162     formatter.write_vertex_index (static_cast<int> (v->parameter_space_in_y()));
00163 
00164     if (! v->has_null_point())
00165     {
00166       // Write the associated point.
00167       formatter.write_vertex_index (1);
00168       formatter.write_point (v->point());
00169 
00170       // Write additional user-defined data.
00171       formatter.write_vertex_data (Vertex_const_handle (v));
00172     }
00173     else
00174     {
00175       // Mark that the vertex is not associated with a point.
00176       formatter.write_vertex_index (0);
00177     }
00178 
00179     formatter.write_vertex_end();
00180     return;
00181   }
00182 
00184   template <class Formatter>
00185   void _write_edge (Formatter& formatter, Edge_const_iterator hit)
00186   {
00187     // Map the halfedge and its twin to their indices.
00188     const DHalfedge   *he = &(*hit);
00189     const DHalfedge   *he_t = he->opposite();
00190 
00191     m_he_index[&(*he)] = m_curr_he;
00192     ++m_curr_he;
00193     m_he_index[&(*he_t)] = m_curr_he;
00194     ++m_curr_he;
00195 
00196     // Write the edge.
00197     formatter.write_edge_begin ();
00198     formatter.write_vertex_index (_index(he_t->vertex()));
00199     formatter.write_vertex_index (_index(he->vertex()));
00200     
00201     if (he->direction() == ARR_LEFT_TO_RIGHT)
00202       formatter.write_vertex_index (0);
00203     else
00204       formatter.write_vertex_index (1);
00205       
00206     if (! he->has_null_curve())
00207     {
00208       // Write the associated curve.
00209       formatter.write_vertex_index (1);
00210       formatter.write_x_monotone_curve (he->curve()); 
00211 
00212       // Write additional user-defined data.
00213       formatter.write_halfedge_data (Halfedge_const_handle (he));
00214       formatter.write_halfedge_data (Halfedge_const_handle (he_t));
00215     }
00216     else
00217     {
00218       // Mark that the edge is fictitious.
00219       formatter.write_vertex_index (0);
00220     }
00221     formatter.write_edge_end ();
00222 
00223     return;
00224   }
00225 
00227   template <class Formatter>
00228   void _write_face (Formatter& formatter, Face_const_iterator fit) const
00229   {
00230     const DFace     *f = &(*fit);
00231 
00232     formatter.write_face_begin();
00233 
00234     // Write whether the face is unbounded and whether it is valid
00235     // (non-fictitious).
00236     if (f->is_unbounded())
00237       formatter.write_vertex_index (1);
00238     else
00239       formatter.write_vertex_index (0);
00240 
00241     if (! f->is_fictitious())
00242       formatter.write_vertex_index (1);
00243     else
00244       formatter.write_vertex_index (0);
00245 
00246     // Write the outer CCBs of the face.
00247     const std::size_t    n_occbs = f->number_of_outer_ccbs();
00248     Outer_ccb_iterator   oc_it;
00249 
00250     formatter.write_outer_ccbs_begin();
00251     formatter.write_size ("number_of_outer_ccbs", n_occbs);
00252     for (oc_it = f->outer_ccbs_begin();
00253          oc_it != f->outer_ccbs_end(); ++oc_it)
00254     {
00255       const std::size_t              n = _circulator_size (*oc_it);
00256 
00257       formatter.write_size ("halfedges_on_outer_ccb", n);
00258       _write_ccb (formatter, *oc_it);      
00259     }
00260     formatter.write_inner_ccbs_end();
00261 
00262     // Write the inner CCBs of the face.
00263     const std::size_t    n_iccbs = f->number_of_inner_ccbs();
00264     Inner_ccb_iterator   ic_it;
00265 
00266     formatter.write_inner_ccbs_begin();
00267     formatter.write_size ("number_of_inner_ccbs", n_iccbs);
00268     for (ic_it = f->inner_ccbs_begin();
00269          ic_it != f->inner_ccbs_end(); ++ic_it)
00270     {
00271       const std::size_t              n = _circulator_size (*ic_it);
00272 
00273       formatter.write_size ("halfedges_on_inner_ccb", n);
00274       _write_ccb (formatter, *ic_it);      
00275     }
00276     formatter.write_inner_ccbs_end();
00277 
00278     // Write the isolated vertices inside the face.
00279     std::size_t               n_isolated = f->number_of_isolated_vertices();
00280     Isolated_vertex_iterator  iso_vit;
00281 
00282     formatter.write_isolated_vertices_begin();
00283     formatter.write_size ("number_of_isolated_vertices", n_isolated);
00284     for (iso_vit = f->isolated_vertices_begin();
00285          iso_vit != f->isolated_vertices_end(); ++iso_vit)
00286     {
00287       formatter.write_vertex_index (_index (&(*iso_vit)));
00288     }
00289     formatter.write_isolated_vertices_end();
00290 
00291     // Write additional user-defined data associated with the face.
00292     if (! f->is_fictitious())
00293       formatter.write_face_data (Face_const_handle (f));
00294 
00295     formatter.write_face_end();
00296 
00297     return;
00298   }
00299 
00301   template <class Formatter>   
00302   void _write_ccb (Formatter& formatter,
00303                    const DHalfedge *ccb) const      
00304   {
00305     const DHalfedge     *curr = ccb;
00306 
00307     formatter.write_ccb_halfedges_begin();
00308     do
00309     {
00310       formatter.write_halfedge_index (_index (curr));
00311       curr = curr->next();
00312     } while (curr != ccb);
00313     formatter.write_ccb_halfedges_end();
00314 
00315     return;
00316   }
00317   
00319   int _index (const DVertex *v) const
00320   {
00321     typename Vertex_index_map::const_iterator   pos = m_v_index.find (v);
00322 
00323     CGAL_assertion (pos != m_v_index.end());
00324     return (pos->second);
00325   }
00326 
00328   int _index (const DHalfedge *he) const
00329   {
00330     typename Halfedge_index_map::const_iterator  pos = m_he_index.find (he);
00331 
00332     CGAL_assertion (pos != m_he_index.end());
00333     return (pos->second);
00334   }
00335 
00337   std::size_t _circulator_size (const DHalfedge *ccb) const
00338   {
00339     CGAL_assertion (ccb != NULL);
00340 
00341     std::size_t       n = 0;
00342     const DHalfedge  *curr = ccb;
00343 
00344     do {
00345       ++n;
00346       curr = curr->next();
00347     } while (curr != ccb);
00348 
00349     return (n);
00350   }  
00351 };
00352 
00353 CGAL_END_NAMESPACE
00354 
00355 #endif // CGAL_IO_ARRANGEMENT_2_WRITER_H 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines