ViennaGrid - The Vienna Grid Library  2.1.0
viennagrid/io/neper_tess_reader.hpp
Go to the documentation of this file.
00001 #ifndef VIENNAGRID_IO_NEPER_TESS_READER_HPP
00002 #define VIENNAGRID_IO_NEPER_TESS_READER_HPP
00003 
00004 /* =======================================================================
00005    Copyright (c) 2011-2014, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008 
00009                             -----------------
00010                      ViennaGrid - The Vienna Grid Library
00011                             -----------------
00012 
00013    License:      MIT (X11), see file LICENSE in the base directory
00014 ======================================================================= */
00015 
00016 
00017 #include <fstream>
00018 #include <iostream>
00019 #include <assert.h>
00020 
00021 #include "viennagrid/forwards.hpp"
00022 #include "viennagrid/mesh/mesh.hpp"
00023 #include "viennagrid/io/helper.hpp"
00024 
00031 namespace viennagrid
00032 {
00033   namespace io
00034   {
00039     struct neper_tess_reader
00040     {
00046       template <typename MeshT>
00047       int operator()(MeshT & mesh_obj, std::string const & filename) const
00048       {
00049         std::vector< std::pair<typename viennagrid::result_of::point<MeshT>::type, int> > seed_points;
00050 
00051         return (*this)(mesh_obj, filename, seed_points);
00052       }
00053 
00060       template <typename MeshT>
00061       void operator()(MeshT & mesh_obj, std::string const & filename,
00062                      std::vector< std::pair<typename viennagrid::result_of::point<MeshT>::type, int> > & seed_points) const
00063       {
00064         typedef typename viennagrid::result_of::point<MeshT>::type           PointType;
00065 
00066         static const int point_dim = viennagrid::result_of::static_size<PointType>::value;
00067 
00068         typedef typename result_of::element<MeshT, vertex_tag>::type         VertexType;
00069         typedef typename result_of::vertex_handle<MeshT>::type          VertexHandleType;
00070         typedef typename VertexType::id_type VertexIDType;
00071 
00072         typedef typename result_of::line_handle<MeshT>::type            LineHandleType;
00073 
00074         std::ifstream reader(filename.c_str());
00075 
00076         #if defined VIENNAGRID_DEBUG_STATUS || defined VIENNAGRID_DEBUG_IO
00077         std::cout << "* poly_reader::operator(): Reading file " << filename << std::endl;
00078         #endif
00079 
00080         if (!reader)
00081         {
00082           throw cannot_open_file_exception("* ViennaGrid: neper_tess_reader::operator(): File " + filename + ": Cannot open file!");
00083         }
00084 
00085         seed_points.clear();
00086 
00087         std::string tmp;
00088         std::istringstream current_line;
00089 
00090 
00091         if (!reader.good())
00092           throw bad_file_format_exception("* ViennaGrid: neper_tess_reader::operator(): File " + filename + " is empty.");
00093 
00094 
00095         std::map<int, VertexHandleType> vertices;
00096         std::map< std::pair<int, int>, LineHandleType > lines;
00097 
00098         while (std::getline(reader, tmp))
00099         {
00100 
00101           if (tmp.find("**vertex") != std::string::npos)
00102           {
00103             std::getline(reader, tmp);
00104 
00105             int num_vertices;
00106             current_line.str(tmp); current_line.clear();
00107             current_line >> num_vertices;
00108 
00109             for (int i = 0; i < num_vertices; ++i)
00110             {
00111               std::getline(reader, tmp);
00112               int vertex_id;
00113               current_line.str(tmp); current_line.clear();
00114               current_line >> vertex_id;
00115 
00116               std::getline(reader, tmp);
00117               std::getline(reader, tmp);
00118 
00119               PointType p;
00120               current_line.str(tmp); current_line.clear();
00121               for (int j = 0; j < std::min(point_dim,3); ++j)
00122                 current_line >> p[j];
00123 
00124               vertices[vertex_id] = viennagrid::make_vertex(mesh_obj, p);
00125             }
00126           }
00127 
00128 
00129 
00130           if (tmp.find("**face") != std::string::npos)
00131           {
00132             std::getline(reader, tmp);
00133 
00134             int num_faces;
00135             current_line.str(tmp); current_line.clear();
00136             current_line >> num_faces;
00137 
00138             for (int i = 0; i < num_faces; ++i)
00139             {
00140               std::getline(reader, tmp);
00141               std::getline(reader, tmp);
00142               std::getline(reader, tmp);
00143               std::getline(reader, tmp);
00144 
00145               int num_vertex_ids;
00146               std::vector<int> vertex_ids;
00147               current_line.str(tmp); current_line.clear();
00148               current_line >> num_vertex_ids;
00149               vertex_ids.resize(num_vertex_ids);
00150 
00151               for (int j = 0; j < num_vertex_ids; ++j)
00152                 current_line >> vertex_ids[j];
00153 
00154               std::vector<LineHandleType> plc_line_handles;
00155               for (int j = 1; j < num_vertex_ids; ++j)
00156               {
00157                 std::pair<int, int> line;
00158                 line.first = std::min( vertex_ids[j-1], vertex_ids[j] );
00159                 line.second = std::max( vertex_ids[j-1], vertex_ids[j] );
00160 
00161                 typename std::map< std::pair<int, int>, LineHandleType >::iterator lit = lines.find( line );
00162                 if (lit != lines.end())
00163                   plc_line_handles.push_back( lit->second );
00164                 else
00165                 {
00166                   LineHandleType l = viennagrid::make_line( mesh_obj, vertices[line.first], vertices[line.second] );
00167                   lines[line] = l;
00168                   plc_line_handles.push_back(l);
00169                 }
00170               }
00171 
00172               {
00173                 std::pair<int, int> line;
00174                 line.first = std::min( vertex_ids.front(), vertex_ids.back() );
00175                 line.second = std::max( vertex_ids.front(), vertex_ids.back() );
00176 
00177                 typename std::map< std::pair<int, int>, LineHandleType >::iterator lit = lines.find( line );
00178                 if (lit != lines.end())
00179                   plc_line_handles.push_back( lit->second );
00180                 else
00181                 {
00182                   LineHandleType l = viennagrid::make_line( mesh_obj, vertices[line.first], vertices[line.second] );
00183                   lines[line] = l;
00184                   plc_line_handles.push_back(l);
00185                 }
00186               }
00187 
00188 
00189 
00190               viennagrid::make_plc( mesh_obj, plc_line_handles.begin(), plc_line_handles.end() );
00191 
00192               std::getline(reader, tmp);
00193               std::getline(reader, tmp);
00194               std::getline(reader, tmp);
00195             }
00196           }
00197 
00198 
00199 
00200           if (tmp.find("**polyhedron") != std::string::npos)
00201           {
00202             std::getline(reader, tmp);
00203 
00204             int num_polyhedrons;
00205             current_line.str(tmp); current_line.clear();
00206             current_line >> num_polyhedrons;
00207 
00208             for (int i = 0; i < num_polyhedrons; ++i)
00209             {
00210               std::getline(reader, tmp);
00211               int polyherdon_id;
00212               current_line.str(tmp); current_line.clear();
00213               current_line >> polyherdon_id;
00214 
00215               PointType p;
00216               for (int j = 0; j < std::min(point_dim,3); ++j)
00217                 current_line >> p[j];
00218 
00219               seed_points.push_back( std::make_pair(p, polyherdon_id) );
00220               std::getline(reader, tmp);
00221               std::getline(reader, tmp);
00222             }
00223           }
00224 
00225         }
00226       } //operator()
00227 
00228     }; //class tetgen_poly_reader
00229 
00230   } //namespace io
00231 } //namespace viennagrid
00232 
00233 #endif