31 Off_reader(std::ifstream& stream) : stream_(stream) { }
50 template<
typename OffVisitor>
51 bool read(OffVisitor& off_visitor) {
52 bool success_read_off_preambule = read_off_preambule(off_visitor);
53 if (!success_read_off_preambule) {
54 std::cerr <<
"could not read off preambule\n";
58 bool success_read_off_points = read_off_points(off_visitor);
59 if (!success_read_off_points) {
60 std::cerr <<
"could not read off points\n";
64 bool success_read_off_faces = read_off_faces(off_visitor);
65 if (!success_read_off_faces) {
66 std::cerr <<
"could not read off faces\n";
71 return success_read_off_preambule && success_read_off_points && success_read_off_faces;
75 std::ifstream& stream_;
86 template<
typename OffVisitor>
87 bool read_off_preambule(OffVisitor& off_visitor) {
89 if (!goto_next_uncomment_line(line))
return false;
91 bool is_off_file = (line.find(
"OFF") != std::string::npos);
92 bool is_noff_file = (line.find(
"nOFF") != std::string::npos);
96 if (!is_off_file && !is_noff_file) {
97 std::cerr << line << std::endl;
98 std::cerr <<
"missing off header\n";
104 stream_ >> off_info_.dim;
109 if (!goto_next_uncomment_line(line))
return false;
110 std::istringstream iss(line);
111 if (!(iss >> off_info_.num_vertices >> off_info_.num_faces >> off_info_.num_edges)) {
112 std::cerr <<
"incorrect number of vertices/faces/edges\n";
115 off_visitor.init(off_info_.dim, off_info_.num_vertices, off_info_.num_faces, off_info_.num_edges);
120 bool goto_next_uncomment_line(std::string& uncomment_line) {
123 if (!std::ifstream::sentry(stream_))
return false;
124 std::getline(stream_, uncomment_line);
125 }
while (uncomment_line[0] ==
'#');
126 return static_cast<bool>(stream_);
129 template<
typename OffVisitor>
130 bool read_off_points(OffVisitor& visitor) {
131 int num_vertices_to_read = off_info_.num_vertices;
132 while (num_vertices_to_read--) {
134 if (!goto_next_uncomment_line(line))
return false;
135 std::vector<double> point;
136 std::istringstream iss(line);
137 point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
139 visitor.point(point);
144 template<
typename OffVisitor>
145 bool read_off_faces(OffVisitor& visitor) {
147 while (goto_next_uncomment_line(line)) {
148 std::istringstream iss(line);
149 int num_face_vertices;
150 iss >> num_face_vertices;
151 std::vector<int> face;
152 face.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
154 visitor.maximal_face(face);
160 template<
typename OFFVisitor>
161 void read_off(
const std::string& name_file_off, OFFVisitor& vis) {
162 std::ifstream stream(name_file_off);
163 if (!stream.is_open()) {
164 std::cerr <<
"could not open file \n";
167 off_reader.
read(vis);
173 #endif // OFF_READER_H_ bool read(OffVisitor &off_visitor)
Read an OFF file and calls the following methods :
Definition: Off_reader.h:51
Definition: SimplicialComplexForAlpha.h:14
OFF file reader top class visitor.
Definition: Off_reader.h:29