summaryrefslogtreecommitdiff
path: root/include/gudhi/Off_reader.h
blob: 05a1e1457ee2d7e6d69bfba90783a3cf59f24448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*    This file is part of the Gudhi Library. The Gudhi library
 *    (Geometric Understanding in Higher Dimensions) is a generic C++
 *    library for computational topology.
 *
 *    Author(s):       David Salinas
 *
 *    Copyright (C) 2014 Inria
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifndef OFF_READER_H_
#define OFF_READER_H_


#include <sstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <fstream>

namespace Gudhi {

/** \brief OFF file reader top class visitor. 
 * 
 * OFF file must be conform to format described here : 
 * http://www.geomview.org/docs/html/OFF.html
 */
class Off_reader {
 public:
  Off_reader(std::ifstream& stream) : stream_(stream) { }

  ~Off_reader() {
    stream_.close();
  }

  /** \brief
   * Read an OFF file and calls the following methods :
   * 
   * <CODE>void init(int dim,int num_vertices,int num_faces,int num_edges);  // from file header - num_edges may not be set
   * 
   * void point(const std::vector<double>& point);  // for each point read
   * 
   * void maximal_face(const std::list<int>& face);  // for each face read
   * 
   * void done();  // upon file read is finished</CODE>
   * 
   * of the visitor when reading a point or a maximal face. Edges are not taken into account.
   */
  template<typename OffVisitor>
  bool read(OffVisitor& off_visitor) {
    bool success_read_off_preambule = read_off_preambule(off_visitor);
    if (!success_read_off_preambule) {
      std::cerr << "could not read off preambule\n";
      return false;
    }

    bool success_read_off_points = read_off_points(off_visitor);
    if (!success_read_off_points) {
      std::cerr << "could not read off points\n";
      return false;
    }

    bool success_read_off_faces = read_off_faces(off_visitor);
    if (!success_read_off_faces) {
      std::cerr << "could not read off faces\n";
      return false;
    }

    off_visitor.done();
    return success_read_off_preambule && success_read_off_points && success_read_off_faces;
  }

 private:
  std::ifstream& stream_;

  struct Off_info {
    int dim;
    int num_vertices;
    int num_edges;
    int num_faces;
  };

  Off_info off_info_;

  template<typename OffVisitor>
  bool read_off_preambule(OffVisitor& off_visitor) {
    std::string line;
    if (!goto_next_uncomment_line(line)) return false;

    bool is_off_file = (line.find("OFF") != std::string::npos);
    bool is_noff_file = (line.find("nOFF") != std::string::npos);



    if (!is_off_file && !is_noff_file) {
      std::cerr << line << std::endl;
      std::cerr << "missing off header\n";
      return false;
    }

    if (is_noff_file) {
      // Should be on a separate line, but we accept it on the same line as the number of vertices
      stream_ >> off_info_.dim;
    } else {
      off_info_.dim = 3;
    }

    if (!goto_next_uncomment_line(line)) return false;
    std::istringstream iss(line);
    if (!(iss >> off_info_.num_vertices >> off_info_.num_faces >> off_info_.num_edges)) {
      std::cerr << "incorrect number of vertices/faces/edges\n";
      return false;
    }
    off_visitor.init(off_info_.dim, off_info_.num_vertices, off_info_.num_faces, off_info_.num_edges);

    return true;
  }

  bool goto_next_uncomment_line(std::string& uncomment_line) {
    do {
      // skip whitespace, including empty lines
      if (!std::ifstream::sentry(stream_)) return false;
      std::getline(stream_, uncomment_line);
    } while (uncomment_line[0] == '#');
    return static_cast<bool>(stream_);
  }

  template<typename OffVisitor>
  bool read_off_points(OffVisitor& visitor) {
    int num_vertices_to_read = off_info_.num_vertices;
    while (num_vertices_to_read--) {
      std::string line;
      if (!goto_next_uncomment_line(line)) return false;
      std::vector<double> point;
      std::istringstream iss(line);
      point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
      // if(point.size() != off_info_.dim) return false;
      visitor.point(point);
    }
    return true;
  }

  template<typename OffVisitor>
  bool read_off_faces(OffVisitor& visitor) {
    std::string line;
    while (goto_next_uncomment_line(line)) {
      std::istringstream iss(line);
      int num_face_vertices;
      iss >> num_face_vertices;
      std::vector<int> face;
      face.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
      // if (face.size() != (off_info_.dim + 1)) return false;
      visitor.maximal_face(face);
    }
    return true;
  }
};

template<typename OFFVisitor>
void read_off(const std::string& name_file_off, OFFVisitor& vis) {
  std::ifstream stream(name_file_off);
  if (!stream.is_open()) {
    std::cerr << "could not open file \n";
  } else {
    Off_reader off_reader(stream);
    off_reader.read(vis);
  }
}

}  // namespace Gudhi

#endif  // OFF_READER_H_