Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
Off_reader.h
1 /*
2  * Off_reader.h
3  * Created on: Nov 28, 2014
4  * This file is part of the Gudhi Library. The Gudhi library
5  * (Geometric Understanding in Higher Dimensions) is a generic C++
6  * library for computational topology.
7  *
8  * Author(s): David Salinas
9  *
10  * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (France)
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 
28 #ifndef GUDHI_OFF_READER_H_
29 #define GUDHI_OFF_READER_H_
30 
31 
32 #include <sstream>
33 #include <iostream>
34 #include <iterator>
35 
36 
37 namespace Gudhi {
38 
51 class Off_reader{
52 public:
53  Off_reader(std::ifstream& stream):stream_(stream){
54  }
55 // Off_reader(const std::string& name):stream_(name){
56 // if(!stream_.is_open())
57 // std::cerr <<"could not open file \n";
58 // }
59 
60  ~Off_reader(){
61  stream_.close();
62  }
63 
72  template<typename OffVisitor>
73  bool read(OffVisitor& off_visitor){
74  bool success_read_off_preambule = read_off_preambule(off_visitor);
75  if(!success_read_off_preambule) {
76  std::cerr <<"could not read off preambule\n";
77  return false;
78  }
79 
80  bool success_read_off_points = read_off_points(off_visitor);
81  if(!success_read_off_points) {
82  std::cerr <<"could not read off points\n";
83  return false;
84  }
85 
86  bool success_read_off_faces = read_off_faces(off_visitor);
87  if(!success_read_off_faces) {
88  std::cerr <<"could not read off faces\n";
89  return false;
90  }
91 
92  off_visitor.done();
93  return success_read_off_preambule && success_read_off_points && success_read_off_faces;
94  }
95 
96 private:
97  std::ifstream& stream_;
98 
99  struct Off_info{
100  int dim;
101  int num_vertices;
102  int num_edges;
103  int num_faces;
104  };
105 
106  Off_info off_info_;
107 
108  template<typename OffVisitor>
109  bool read_off_preambule(OffVisitor& off_visitor){
110  std::string line;
111  if(!goto_next_uncomment_line(line)) return false;
112 
113  bool is_off_file = (line.find("OFF") != std::string::npos);
114  bool is_noff_file = (line.find("nOFF") != std::string::npos);
115 
116  if(!is_off_file && !is_noff_file) {
117  std::cerr << line<<std::endl;
118  std::cerr << "missing off header\n";
119  return false;
120  }
121 
122  if(!goto_next_uncomment_line(line)) return false;
123  std::istringstream iss(line);
124  if(is_off_file){
125  off_info_.dim = 3;
126  if(!(iss >> off_info_.num_vertices >> off_info_.num_faces >> off_info_.num_edges)){
127  std::cerr << "incorrect number of vertices/faces/edges\n";
128  return false;
129  }
130  }
131  else
132  if(!(iss >> off_info_.dim >> off_info_.num_vertices >> off_info_.num_faces >> off_info_.num_edges)){
133  std::cerr << "incorrect number of vertices/faces/edges\n";
134  return false;
135  }
136  off_visitor.init(off_info_.dim,off_info_.num_vertices,off_info_.num_faces,off_info_.num_edges);
137 
138  return true;
139  }
140 
141  bool goto_next_uncomment_line(std::string& uncomment_line){
142  uncomment_line.clear();
143  do
144  std::getline(stream_, uncomment_line);
145  while(uncomment_line[0] == '%');// || uncomment_line.empty());
146  return (uncomment_line.size()>0 && uncomment_line[0] != '%');
147  }
148 
149 
150  template<typename OffVisitor>
151  bool read_off_points(OffVisitor& visitor){
152  int num_vertices_to_read = off_info_.num_vertices;
153  while(num_vertices_to_read--){
154  std::string line;
155  if(!goto_next_uncomment_line(line)) return false;
156  std::vector<double> point;
157  std::istringstream iss(line);
158  point.assign(std::istream_iterator<double>(iss),std::istream_iterator<double>());
159 // if(point.size() != off_info_.dim) return false;
160  visitor.point(point);
161  }
162  return true;
163  }
164 
165  template<typename OffVisitor>
166  bool read_off_faces(OffVisitor& visitor){
167  std::string line;
168  while(goto_next_uncomment_line(line)){
169  std::istringstream iss(line);
170  int num_face_vertices;
171  iss >> num_face_vertices;
172  std::vector<int> face;
173  face.assign(std::istream_iterator<int>(iss),std::istream_iterator<int>());
174  if(!face.size() == off_info_.num_vertices) return false;
175  visitor.maximal_face(face);
176  }
177  return true;
178  }
179 };
180 
181 
182 template<typename OFFVisitor>
183 void read_off(const std::string& name_file_off,OFFVisitor& vis){
184  std::ifstream stream(name_file_off);
185  if(!stream.is_open())
186  std::cerr <<"could not open file \n";
187  else{
188  Off_reader off_reader(stream);
189  off_reader.read(vis);
190  }
191 }
192 
193 
194 
195 } // namespace Gudhi
196 
197 
198 #endif /* GUDHI_OFF_READER_H_ */
bool read(OffVisitor &off_visitor)
Definition: Off_reader.h:73
Definition: Off_reader.h:51