ViennaGrid - The Vienna Grid Library  2.1.0
viennagrid/algorithm/extract_seed_points.hpp
Go to the documentation of this file.
00001 #ifndef VIENNAGRID_ALGORITHM_EXTRACT_SEED_POINTS_HPP
00002 #define VIENNAGRID_ALGORITHM_EXTRACT_SEED_POINTS_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 #include "viennagrid/algorithm/centroid.hpp"
00017 #include "viennagrid/mesh/neighbor_iteration.hpp"
00018 
00023 namespace viennagrid
00024 {
00025   namespace detail
00026   {
00028     template<typename MeshT, typename UnvisitedCellMapT>
00029     void neighbor_mark( MeshT const & mesh, UnvisitedCellMapT & unvisitied_cells )
00030     {
00031       bool found = true;
00032       while (found)
00033       {
00034         found = false;
00035 
00036         for (typename UnvisitedCellMapT::iterator ucit = unvisitied_cells.begin(); ucit != unvisitied_cells.end(); )
00037         {
00038           typedef typename viennagrid::result_of::cell_tag<MeshT>::type CellTagType;
00039           typedef typename viennagrid::result_of::facet_tag<MeshT>::type FacetTagType;
00040           typedef typename viennagrid::result_of::const_neighbor_range<MeshT, CellTagType, FacetTagType>::type NeighborRangeType;
00041           typedef typename viennagrid::result_of::iterator<NeighborRangeType>::type NeighborIteratorType;
00042 
00043           NeighborRangeType neighbors( mesh, ucit->second );
00044           NeighborIteratorType ncit = neighbors.begin();
00045           for (; ncit != neighbors.end(); ++ncit)
00046           {
00047             typename UnvisitedCellMapT::iterator ucit2 = unvisitied_cells.find( ncit->id() );
00048             if (ucit2 == unvisitied_cells.end())
00049               break;
00050           }
00051 
00052           if (ncit != neighbors.end())
00053           {
00054             found = true;
00055             unvisitied_cells.erase( ucit++ );
00056           }
00057           else
00058             ++ucit;
00059         }
00060       }
00061     }
00062   }
00063 
00069   template<typename MeshSegmentT, typename SeedPointContainerT>
00070   void extract_seed_points( MeshSegmentT const & mesh, SeedPointContainerT & seed_points )
00071   {
00072     typedef typename viennagrid::result_of::cell_id<MeshSegmentT>::type CellIDType;
00073     typedef typename viennagrid::result_of::const_cell_handle<MeshSegmentT>::type ConstCellHandleType;
00074 
00075     typedef typename viennagrid::result_of::const_cell_range<MeshSegmentT>::type CellRangeType;
00076     typedef typename viennagrid::result_of::iterator<CellRangeType>::type CellIteratorType;
00077 
00078     CellRangeType cells(mesh);
00079 
00080     if (!cells.empty())
00081     {
00082       typedef std::map<CellIDType, ConstCellHandleType> UnvisitedCellMapType;
00083       UnvisitedCellMapType unvisited_cells;
00084 
00085       for (CellIteratorType cit = cells.begin(); cit != cells.end(); ++cit)
00086         unvisited_cells[ cit->id() ] = cit.handle();
00087 
00088       while (!unvisited_cells.empty())
00089       {
00090         for (CellIteratorType cit = cells.begin(); cit != cells.end(); ++cit)
00091         {
00092           typename UnvisitedCellMapType::iterator ucit = unvisited_cells.find( cit->id() );
00093           if (ucit == unvisited_cells.end())
00094             continue;
00095 
00096           seed_points.push_back( viennagrid::centroid(*cit) );
00097           unvisited_cells.erase( ucit );
00098 
00099           neighbor_mark( mesh, unvisited_cells );
00100         }
00101       }
00102     }
00103   }
00104 
00111   template<typename MeshT, typename SegmentationT, typename SeedPointContainerT>
00112   void extract_seed_points( MeshT const & mesh, SegmentationT const & segmentation, SeedPointContainerT & seed_points )
00113   {
00114     typedef typename viennagrid::result_of::point<MeshT>::type PointType;
00115 
00116     if (segmentation.empty())
00117     {
00118       std::vector<PointType> points;
00119       extract_seed_points(mesh, points);
00120       for (unsigned int i = 0; i < points.size(); ++i)
00121         seed_points.push_back( std::make_pair(points[i], 0) );
00122     }
00123     else
00124     {
00125       for (typename SegmentationT::const_iterator sit = segmentation.begin(); sit != segmentation.end(); ++sit)
00126       {
00127         std::vector<PointType> points;
00128         extract_seed_points( *sit, points );
00129         for (unsigned int i = 0; i < points.size(); ++i)
00130           seed_points.push_back( std::make_pair(points[i], sit->id()) );
00131       }
00132     }
00133   }
00134 }
00135 
00136 
00137 #endif