ViennaGrid - The Vienna Grid Library  2.1.0
viennagrid/io/netgen_reader.hpp
Go to the documentation of this file.
00001 #ifndef VIENNAGRID_IO_NETGEN_READER_HPP
00002 #define VIENNAGRID_IO_NETGEN_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 #include "viennagrid/forwards.hpp"
00021 
00022 #include "viennagrid/mesh/mesh.hpp"
00023 #include "viennagrid/mesh/segmentation.hpp"
00024 #include "viennagrid/mesh/element_creation.hpp"
00025 
00026 #include "viennagrid/io/helper.hpp"
00027 
00032 namespace viennagrid
00033 {
00034   namespace io
00035   {
00036 
00038     struct netgen_reader
00039     {
00046       template <typename MeshType, typename SegmentationType>
00047       void operator()(MeshType & mesh_obj, SegmentationType & segmentation, std::string const & filename) const
00048       {
00049         typedef typename viennagrid::result_of::point<MeshType>::type    PointType;
00050 
00051         const int point_dim = viennagrid::result_of::static_size<PointType>::value;
00052 
00053         typedef typename result_of::cell_tag<MeshType>::type CellTag;
00054         typedef typename result_of::element<MeshType, CellTag>::type CellType;
00055 
00056         typedef typename result_of::element<MeshType, vertex_tag>::type                           VertexType;
00057         typedef typename result_of::handle<MeshType, vertex_tag>::type                           VertexHandleType;
00058 
00059         std::ifstream reader(filename.c_str());
00060 
00061         #if defined VIENNAGRID_DEBUG_STATUS || defined VIENNAGRID_DEBUG_IO
00062         std::cout << "* netgen_reader::operator(): Reading file " << filename << std::endl;
00063         #endif
00064 
00065         if (!reader)
00066         {
00067           throw cannot_open_file_exception("* ViennaGrid: netgen_reader::operator(): File " + filename + ": Cannot open file!");
00068         }
00069 
00070         long node_num = 0;
00071         long cell_num = 0;
00072 
00073         if (!reader.good())
00074           throw bad_file_format_exception("* ViennaGrid: netgen_reader::operator(): File " + filename + " is empty.");
00075 
00076         //
00077         // Read vertices:
00078         //
00079         reader >> node_num;
00080         assert(node_num > 0);
00081 
00082         #if defined VIENNAGRID_DEBUG_STATUS || defined VIENNAGRID_DEBUG_IO
00083         std::cout << "* netgen_reader::operator(): Reading " << node_num << " vertices... " << std::endl;
00084         #endif
00085 
00086 
00087 
00088         for (int i=0; i<node_num; i++)
00089         {
00090           if (!reader.good())
00091             throw bad_file_format_exception("* ViennaGrid: netgen_reader::operator(): File " + filename + ": EOF encountered while reading vertices.");
00092 
00093           PointType p;
00094 
00095           for (std::size_t j=0; j<static_cast<std::size_t>(point_dim); j++)
00096             reader >> p[j];
00097 
00098           viennagrid::make_vertex_with_id( mesh_obj, typename VertexType::id_type(i), p );
00099         }
00100 
00101         if (!reader.good())
00102           throw bad_file_format_exception("* ViennaGrid: netgen_reader::operator(): File " + filename + ": EOF encountered when reading number of cells.");
00103 
00104 
00105         //
00106         // Read cells:
00107         //
00108         reader >> cell_num;
00109 
00110         #if defined VIENNAGRID_DEBUG_STATUS || defined VIENNAGRID_DEBUG_IO
00111         std::cout << "* netgen_reader::operator(): Reading " << cell_num << " cells... " << std::endl;
00112         #endif
00113 
00114         for (int i=0; i<cell_num; ++i)
00115         {
00116           std::size_t vertex_num;
00117           viennagrid::static_array<VertexHandleType, boundary_elements<CellTag, vertex_tag>::num> cell_vertex_handles;
00118 
00119           if (!reader.good())
00120             throw bad_file_format_exception("* ViennaGrid: netgen_reader::operator(): File " + filename + ": EOF encountered while reading cells (segment index expected).");
00121 
00122           int segment_index;
00123           reader >> segment_index;
00124 
00125           for (std::size_t j=0; j<static_cast<std::size_t>(boundary_elements<CellTag, vertex_tag>::num); ++j)
00126           {
00127             if (!reader.good())
00128               throw bad_file_format_exception("* ViennaGrid: netgen_reader::operator(): File " + filename + ": EOF encountered while reading cells (cell ID expected).");
00129 
00130             reader >> vertex_num;
00131             cell_vertex_handles[j] = viennagrid::vertices(mesh_obj).handle_at(vertex_num-1);
00132           }
00133 
00134           viennagrid::make_element_with_id<CellType>(segmentation[segment_index], cell_vertex_handles.begin(), cell_vertex_handles.end(), typename CellType::id_type(i));
00135         }
00136       } //operator()
00137 
00138 
00144       template <typename MeshType>
00145       void operator()(MeshType & mesh_obj, std::string const & filename)
00146       {
00147         typedef typename viennagrid::result_of::segmentation<MeshType>::type SegmentationType;
00148         SegmentationType tmp(mesh_obj);
00149         (*this)(mesh_obj, tmp, filename);
00150       }
00151 
00152     }; //class netgen_reader
00153 
00154   } //namespace io
00155 } //namespace viennagrid
00156 
00157 #endif