Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
reader_utils.h
1  /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Clément Maria
6  *
7  * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (France)
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef GUDHI_READER_UTILS_H
24 #define GUDHI_READER_UTILS_H
25 
26 #include <iostream>
27 #include <fstream>
28 #include <boost/graph/adjacency_list.hpp>
29 #include "gudhi/graph_simplicial_complex.h"
30 
40 inline void
41 read_points ( std::string file_name
42  , std::vector< std::vector< double > > & points)
43 {
44  std::ifstream in_file (file_name.c_str(),std::ios::in);
45  if(!in_file.is_open()) {
46  std::cerr << "Unable to open file " << file_name << std::endl;
47  return;}
48 
49  std::string line;
50  double x;
51  while( getline ( in_file , line ) )
52  {
53  std::vector< double > point;
54  std::istringstream iss( line );
55  while(iss >> x) { point.push_back(x); }
56  points.push_back(point);
57  }
58  in_file.close();
59 }
60 
73 inline Graph_t
74 read_graph ( std::string file_name )
75 {
76  std::ifstream in_ (file_name.c_str(),std::ios::in);
77  if(!in_.is_open()) { std::cerr << "Unable to open file " << file_name << std::endl; }
78 
79  std::vector< Edge_t > edges;
80  std::vector< Filtration_value > edges_fil;
81  std::map< Vertex_handle, Filtration_value > vertices;
82 
83  std::string line;
84  int dim;
85  Vertex_handle u,v,max_h = -1;
86  Filtration_value fil;
87  while( getline ( in_ , line ) )
88  {
89  std::istringstream iss( line );
90  while(iss >> dim) {
91  switch ( dim ) {
92  case 0 : {
93  iss >> u; iss >> fil;
94  vertices[u] = fil;
95  if(max_h < u) { max_h = u; }
96  break;
97  }
98  case 1 : {
99  iss >> u; iss >> v; iss >> fil;
100  edges.push_back(Edge_t(u,v));
101  edges_fil.push_back(fil);
102  break;
103  }
104  default: {break;}
105  }
106  }
107  }
108  in_.close();
109 
110  if((size_t)(max_h+1) != vertices.size())
111  { std::cerr << "Error: vertices must be labeled from 0 to n-1 \n"; }
112 
113  Graph_t skel_graph(edges.begin(),edges.end(),edges_fil.begin(),vertices.size());
114  auto vertex_prop = boost::get(vertex_filtration_t(),skel_graph);
115 
116  boost::graph_traits<Graph_t>::vertex_iterator vi, vi_end;
117  auto v_it = vertices.begin();
118  for (tie(vi, vi_end) = boost::vertices(skel_graph); vi != vi_end; ++vi,++v_it)
119  { boost::put(vertex_prop, *vi, v_it->second); }
120 
121  return skel_graph;
122 }
123 
136 template< typename Vertex_handle
137  , typename Filtration_value >
138 bool read_simplex ( std::istream & in_
139  , std::vector< Vertex_handle > & simplex
140  , Filtration_value & fil )
141 {
142  int dim=0;
143  if(!(in_ >> dim)) return false;
144  Vertex_handle v;
145  for(int i=0; i<dim+1; ++i)
146  { in_ >> v; simplex.push_back(v); }
147  in_ >> fil;
148  in_.ignore((std::numeric_limits<std::streamsize>::max)(), '\n'); // ignore until the carriage return
149  return true;
150 }
151 
165 template< typename Simplex_key
166  , typename Filtration_value >
167 bool read_hasse_simplex ( std::istream & in_
168  , std::vector< Simplex_key > & boundary
169  , Filtration_value & fil )
170 {
171  int dim;
172  if(!(in_ >> dim)) return false;
173  if(dim == 0) {in_ >> fil; return true;}
174  Simplex_key key;
175  for(int i=0; i<dim+1; ++i)
176  { in_ >> key; boundary.push_back(key); }
177  in_ >> fil;
178  return true;
179 }
180 
181 #endif // GUDHI_READER_UTILS_H