BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/IO/Arr_text_formatter.h
Go to the documentation of this file.
00001 // Copyright (c) 2005  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/Arr_text_formatter.h $
00015 // $Id: Arr_text_formatter.h 50387 2009-07-06 11:25:27Z 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 #ifndef CGAL_ARR_TEXT_FORMATTER_H
00021 #define CGAL_ARR_TEXT_FORMATTER_H
00022 
00027 #include <CGAL/basic.h>
00028 #include <iostream>
00029 
00030 CGAL_BEGIN_NAMESPACE
00031 
00037 template <class Arrangement_>
00038 class Arr_text_formatter
00039 {
00040 
00041 public:
00042 
00043   typedef Arrangement_                                   Arrangement_2;
00044   typedef typename Arrangement_2::Size                   Size;
00045   typedef typename Arrangement_2::Dcel                   Dcel;
00046   typedef typename Arrangement_2::X_monotone_curve_2     X_monotone_curve_2;
00047   typedef typename Arrangement_2::Point_2                Point_2;
00048 
00049   typedef typename Arrangement_2::Vertex_handle          Vertex_handle;
00050   typedef typename Arrangement_2::Halfedge_handle        Halfedge_handle;
00051   typedef typename Arrangement_2::Face_handle            Face_handle;
00052 
00053   typedef typename Arrangement_2::Vertex_const_handle    Vertex_const_handle;
00054   typedef typename Arrangement_2::Halfedge_const_handle  Halfedge_const_handle;
00055   typedef typename Arrangement_2::Face_const_handle      Face_const_handle;
00056  
00057 protected:
00058 
00059   typedef typename Dcel::Vertex                           DVertex;
00060   typedef typename Dcel::Halfedge                         DHalfedge;
00061   typedef typename Dcel::Face                             DFace;
00062   
00063   // Data members:
00064   std::ostream  *m_out;
00065   IO::Mode       m_old_out_mode;
00066   std::istream  *m_in;
00067   IO::Mode       m_old_in_mode;
00068 
00069 public:  
00070 
00072   Arr_text_formatter ():
00073     m_out (NULL),
00074     m_in (NULL)
00075   {}
00076 
00078   Arr_text_formatter (std::ostream& os) :
00079     m_out (&os),
00080     m_in(NULL)
00081   {}
00082 
00084   Arr_text_formatter (std::istream& is) :
00085     m_out(NULL),
00086     m_in(&is)
00087   {}
00088 
00090   virtual ~Arr_text_formatter()
00091   {}
00092 
00094   void set_out (std::ostream& os)
00095   {
00096     m_out = &os;
00097   }
00098 
00100   void set_in (std::istream& is)
00101   {
00102     m_in = &is;
00103   }
00104 
00106   inline std::ostream& out ()
00107   {
00108     CGAL_assertion (m_out != NULL);
00109     return (*m_out);
00110   }
00111 
00113   inline std::istream& in ()
00114   {
00115     CGAL_assertion (m_in != NULL);
00116     return (*m_in);
00117   }
00118 
00120 
00121 
00123   void write_arrangement_begin ()
00124   {
00125     CGAL_assertion (m_out != NULL);
00126     m_old_out_mode = get_mode(*m_out);
00127     set_ascii_mode (*m_out);
00128     _write_comment ("BEGIN ARRANGEMENT");
00129   }
00130 
00132   void write_arrangement_end()
00133   {
00134     _write_comment ("END ARRANGEMENT");
00135     set_mode (*m_out, m_old_out_mode);
00136   }
00137 
00139   void write_size (const char *label, Size size)
00140   { 
00141     _write_comment (label);
00142     out() << size << '\n';
00143   }
00144 
00146   void write_vertices_begin ()
00147   {
00148     _write_comment ("BEGIN VERTICES");
00149   }
00150 
00152   void write_vertices_end ()
00153   {
00154     _write_comment ("END VERTICES");
00155   }
00156 
00158   void write_edges_begin ()
00159   {
00160     _write_comment ("BEGIN EDGES");
00161   }
00162 
00164   void write_edges_end ()
00165   {
00166     _write_comment ("END EDGES");
00167   }
00168 
00170   void write_faces_begin ()
00171   {
00172     _write_comment ("BEGIN FACES");
00173   }
00174 
00176   void write_faces_end ()
00177   {
00178     _write_comment ("END FACES");
00179   }
00181 
00183 
00184   void write_vertex_begin ()
00185   {}
00186 
00187   void write_vertex_end ()
00188   {
00189     out() << std::endl;
00190   }
00191   
00192   virtual void write_point (const Point_2& p)
00193   {
00194     out() << p;
00195   }
00196 
00197   virtual void write_vertex_data (Vertex_const_handle )
00198   {}
00200 
00202 
00203   void write_edge_begin ()
00204   {}
00205 
00206   void write_edge_end ()
00207   {
00208     out() << std::endl;
00209   }
00210 
00211   void write_vertex_index (int idx)
00212   {
00213     out() << idx << ' ';
00214   }
00215 
00216   virtual void write_x_monotone_curve (const X_monotone_curve_2& cv)
00217   {
00218     out() << cv;
00219   }
00220 
00221   virtual void write_halfedge_data (Halfedge_const_handle )
00222   {}
00224 
00226 
00227   void write_face_begin ()
00228   {
00229     _write_comment ("BEGIN FACE");
00230   }
00231 
00232   void write_face_end ()
00233   {
00234     _write_comment ("END FACE");
00235   }
00236 
00237   void write_outer_ccbs_begin ()
00238   {
00239     out() << std::endl;
00240   }
00241 
00242   void write_outer_ccbs_end ()
00243   {}
00244 
00245   void write_inner_ccbs_begin ()
00246   {}
00247 
00248   void write_inner_ccbs_end ()
00249   {}
00250 
00251   virtual void write_face_data (Face_const_handle )
00252   {}
00253 
00254   void write_ccb_halfedges_begin()
00255   {}
00256   
00257   void write_ccb_halfedges_end()
00258   {
00259     out() << std::endl;
00260   }
00261 
00262   void write_halfedge_index (int idx)
00263   {
00264     out() << idx << ' ';
00265   }
00266 
00267   void write_isolated_vertices_begin ()
00268   {}
00269 
00270   void write_isolated_vertices_end ()
00271   {
00272     out() << std::endl;
00273   }
00275 
00277 
00278 
00280   void read_arrangement_begin () 
00281   {
00282     CGAL_assertion (m_in != NULL);
00283     m_old_in_mode = get_mode(*m_in);
00284     set_ascii_mode(*m_in);
00285     _skip_comments();
00286   }
00287 
00289   void read_arrangement_end() 
00290   {
00291     _skip_comments();
00292     set_mode(*m_in, m_old_in_mode);
00293   }
00294 
00296   Size read_size (const char* /* title */ = NULL)
00297   {
00298     std::size_t   val;
00299 
00300     _skip_comments();
00301     in() >> val;
00302     _skip_until_EOL();
00303 
00304     return (val);
00305   }
00306 
00308   void read_vertices_begin()
00309   {
00310     _skip_comments();
00311   }
00312 
00313   void read_vertices_end()
00314   {
00315     _skip_comments();
00316   }
00317 
00319   void read_edges_begin()
00320   {
00321     _skip_comments();
00322   }
00323 
00324   void read_edges_end()
00325   {
00326     _skip_comments();
00327   }
00328 
00330   void read_faces_begin()
00331   {
00332     _skip_comments();
00333   }
00334 
00335   void read_faces_end()
00336   {
00337     _skip_comments();
00338   }
00340 
00342 
00343   void read_vertex_begin ()
00344   {}
00345   
00346   void read_vertex_end ()
00347   {}
00348 
00349   virtual void read_point (Point_2& p) 
00350   {
00351     in() >> p;
00352     _skip_until_EOL();
00353   }
00354 
00355   virtual void read_vertex_data (Vertex_handle )
00356   {}
00358 
00360 
00361   void read_edge_begin ()
00362   {}
00363   
00364   void read_edge_end ()
00365   {}
00366   
00367   int read_vertex_index () 
00368   {
00369     int  val = 0;
00370     in() >> val;
00371     return (val);
00372   }
00373 
00374   virtual void read_x_monotone_curve (X_monotone_curve_2& cv) 
00375   {
00376     in() >> cv;
00377     _skip_until_EOL();
00378   }
00379 
00380   virtual void read_halfedge_data (Halfedge_handle )
00381   {}
00382  
00384 
00385   void read_face_begin ()
00386   {
00387     _skip_comments();
00388   }
00389   
00390   void read_face_end ()
00391   {
00392     _skip_comments();
00393   }
00394 
00395   void read_outer_ccbs_begin ()
00396   {}
00397   
00398   void read_outer_ccbs_end ()
00399   {}
00400 
00401   int read_halfedge_index ()
00402   { 
00403     int  val = 0;
00404     in() >> val;
00405     return (val);
00406   }
00407 
00408   void read_inner_ccbs_begin ()
00409   {}
00410   
00411   void read_inner_ccbs_end ()
00412   {}
00413 
00414   void read_ccb_halfedges_begin()
00415   {}
00416   
00417   void read_ccb_halfedges_end() 
00418   {
00419     _skip_until_EOL ();
00420   }
00421 
00422   void read_isolated_vertices_begin ()
00423   {}
00424   
00425   void read_isolated_vertices_end () 
00426   {
00427     _skip_until_EOL ();
00428   }
00429 
00430   virtual void read_face_data (Face_handle )
00431   {}
00433 
00434 protected:
00435 
00437   void _write_comment (const char *str)
00438   {
00439     out() << "# " << str << std::endl;
00440   }
00441 
00443   void _skip_until_EOL () 
00444   {
00445     CGAL_assertion (m_in != NULL);
00446 
00447     int     c;
00448     while ((c = m_in->get()) != EOF && c != '\n') {};
00449   }
00450   
00452   void _skip_comments () 
00453   {
00454     CGAL_assertion (m_in != NULL);
00455 
00456     int     c = m_in->get();
00457     if (c == ' ')
00458     {
00459       // Skip blanks until EOL.
00460       while ((c = m_in->get()) != EOF && c == ' ') {};
00461       if (c != '\n')
00462       {
00463         m_in->putback (c);
00464         return;
00465       }
00466       else
00467       {
00468         c = m_in->get();
00469       }
00470     }
00471 
00472     // Skip comment lines that begin with a '#' character.
00473     while (c != EOF && c == '#')
00474     {
00475       _skip_until_EOL();
00476       c = m_in->get();
00477     }
00478     m_in->putback (c);
00479   }
00480 };
00481 
00487 template <class Arrangement_>
00488 class Arr_face_extended_text_formatter :
00489   public Arr_text_formatter<Arrangement_>
00490 {
00491 
00492 public:
00493 
00494   typedef Arrangement_                               Arrangement_2;
00495   typedef Arr_text_formatter<Arrangement_2>          Base;
00496 
00497   typedef typename Base::Size                        Size;
00498   typedef typename Base::Dcel                        Dcel;
00499   typedef typename Arrangement_2::X_monotone_curve_2 X_monotone_curve_2;
00500   typedef typename Arrangement_2::Point_2            Point_2;
00501 
00502   typedef typename Base::Vertex_handle               Vertex_handle;
00503   typedef typename Base::Halfedge_handle             Halfedge_handle;
00504   typedef typename Base::Face_handle                 Face_handle;
00505 
00506   typedef typename Base::Vertex_const_handle         Vertex_const_handle;
00507   typedef typename Base::Halfedge_const_handle       Halfedge_const_handle;
00508   typedef typename Base::Face_const_handle           Face_const_handle;
00509 
00511   Arr_face_extended_text_formatter () :
00512     Base ()
00513   {}
00514 
00516   Arr_face_extended_text_formatter (std::ostream& os) :
00517     Base (os)
00518   {}
00519 
00521   Arr_face_extended_text_formatter (std::istream& is) :
00522     Base (is)
00523   {}
00524 
00526   virtual void write_face_data (Face_const_handle f)
00527   {
00528     this->out() << f->data() << '\n';
00529   }
00530   
00532   virtual void read_face_data (Face_handle f)
00533   {
00534     this->in() >> f->data();
00535     this->_skip_until_EOL();
00536   }
00537 };
00538 
00544 template <class Arrangement_>
00545 class Arr_extended_dcel_text_formatter :
00546   public Arr_text_formatter<Arrangement_>
00547 {
00548 public:
00549 
00550   typedef Arrangement_                               Arrangement_2;
00551   typedef Arr_text_formatter<Arrangement_2>          Base;
00552 
00553   typedef typename Base::Size                        Size;
00554   typedef typename Base::Dcel                        Dcel;
00555   typedef typename Arrangement_2::X_monotone_curve_2 X_monotone_curve_2;
00556   typedef typename Arrangement_2::Point_2            Point_2;
00557 
00558   typedef typename Base::Vertex_handle               Vertex_handle;
00559   typedef typename Base::Halfedge_handle             Halfedge_handle;
00560   typedef typename Base::Face_handle                 Face_handle;
00561 
00562   typedef typename Base::Vertex_const_handle         Vertex_const_handle;
00563   typedef typename Base::Halfedge_const_handle       Halfedge_const_handle;
00564   typedef typename Base::Face_const_handle           Face_const_handle;
00565 
00567   Arr_extended_dcel_text_formatter () :
00568     Base ()
00569   {}
00570 
00572   Arr_extended_dcel_text_formatter (std::ostream& os) :
00573     Base (os)
00574   {}
00575 
00577   Arr_extended_dcel_text_formatter (std::istream& is) :
00578     Base (is)
00579   {}
00580 
00582   virtual void write_vertex_data (Vertex_const_handle v)
00583   {
00584     this->out() << '\n' << v->data();
00585   }
00586   
00588   virtual void read_vertex_data (Vertex_handle v)
00589   {
00590     this->in() >> v->data();
00591     this->_skip_until_EOL();
00592   }
00593 
00595   virtual void write_halfedge_data (Halfedge_const_handle he)
00596   {
00597     this->out() << '\n' << he->data();
00598   }
00599   
00601   virtual void read_halfedge_data (Halfedge_handle he)
00602   {
00603     this->in() >> he->data();
00604     this->_skip_until_EOL();
00605   }
00606 
00608   virtual void write_face_data (Face_const_handle f)
00609   {
00610     this->out() << f->data() << '\n';
00611   }
00612   
00614   virtual void read_face_data (Face_handle f)
00615   {
00616     this->in() >> f->data();
00617     this->_skip_until_EOL();
00618   }
00619 };
00620 
00621 CGAL_END_NAMESPACE
00622 
00623 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines