Skeleton_blocker_off_io.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): David Salinas
6  *
7  * Copyright (C) 2014 Inria
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 SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
24 #define SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
25 
26 #include <gudhi/Off_reader.h>
27 
28 #include <string>
29 #include <vector>
30 #include <map>
31 
32 namespace Gudhi {
33 
34 namespace skeleton_blocker {
35 
39 template<typename Complex>
41  Complex& complex_;
42  typedef typename Complex::Vertex_handle Vertex_handle;
43  typedef typename Complex::Point Point;
44 
45  const bool load_only_points_;
46 
47  public:
48  explicit Skeleton_blocker_off_flag_visitor_reader(Complex& complex, bool load_only_points = false) :
49  complex_(complex),
50  load_only_points_(load_only_points) { }
51 
52  void init(int dim, int num_vertices, int num_faces, int num_edges) {
53  // TODO(DS): do an assert to check that this number are correctly read
54  // TODO(DS): reserve size for vector points
55  }
56 
57  void point(const std::vector<double>& point) {
58  complex_.add_vertex(Point(point.begin(), point.end()));
59  }
60 
61  void maximal_face(const std::vector<int>& face) {
62  if (!load_only_points_) {
63  for (size_t i = 0; i < face.size(); ++i)
64  for (size_t j = i + 1; j < face.size(); ++j) {
65  complex_.add_edge_without_blockers(Vertex_handle(face[i]), Vertex_handle(face[j]));
66  }
67  }
68  }
69 
70  void done() { }
71 };
72 
76 template<typename Complex>
78  Complex& complex_;
79  typedef typename Complex::Vertex_handle Vertex_handle;
80  typedef typename Complex::Simplex Simplex;
81  typedef typename Complex::Point Point;
82 
83  const bool load_only_points_;
84  std::vector<Point> points_;
85  std::vector<Simplex> maximal_faces_;
86 
87  public:
88  explicit Skeleton_blocker_off_visitor_reader(Complex& complex, bool load_only_points = false) :
89  complex_(complex),
90  load_only_points_(load_only_points) { }
91 
92  void init(int dim, int num_vertices, int num_faces, int num_edges) {
93  maximal_faces_.reserve(num_faces);
94  points_.reserve(num_vertices);
95  }
96 
97  void point(const std::vector<double>& point) {
98  points_.emplace_back(point.begin(), point.end());
99  }
100 
101  void maximal_face(const std::vector<int>& face) {
102  if (!load_only_points_) {
103  Simplex s;
104  for (auto x : face)
105  s.add_vertex(Vertex_handle(x));
106  maximal_faces_.emplace_back(s);
107  }
108  }
109 
110  void done() {
111  complex_ = make_complex_from_top_faces<Complex>(maximal_faces_.begin(), maximal_faces_.end(),
112  points_.begin(), points_.end());
113  }
114 };
115 
119 template<typename Complex>
121  public:
127  Skeleton_blocker_off_reader(const std::string & name_file, Complex& read_complex,
128  bool read_only_points = false, bool is_flag = false) : valid_(false) {
129  std::ifstream stream(name_file);
130  if (stream.is_open()) {
131  if (is_flag || read_only_points) {
132  Skeleton_blocker_off_flag_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
133  Off_reader off_reader(stream);
134  valid_ = off_reader.read(off_visitor);
135  } else {
136  Skeleton_blocker_off_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
137  Off_reader off_reader(stream);
138  valid_ = off_reader.read(off_visitor);
139  }
140  }
141  }
142 
146  bool is_valid() const {
147  return valid_;
148  }
149 
150  private:
151  bool valid_;
152 };
153 
154 template<typename Complex>
155 class Skeleton_blocker_off_writer {
156  public:
162  Skeleton_blocker_off_writer(const std::string & name_file, const Complex& save_complex) {
163  typedef typename Complex::Vertex_handle Vertex_handle;
164 
165  std::ofstream stream(name_file);
166  if (stream.is_open()) {
167  stream << "OFF\n";
168  size_t num_triangles = std::distance(save_complex.triangle_range().begin(), save_complex.triangle_range().end());
169  stream << save_complex.num_vertices() << " " << num_triangles << " 0 \n";
170 
171  // in case the complex has deactivated some vertices, eg only has vertices 0 2 5 7 for instance
172  // we compute a map from 0 2 5 7 to 0 1 2 3
173  std::map<Vertex_handle, size_t> vertex_num;
174  size_t current_vertex = 0;
175 
176  for (auto v : save_complex.vertex_range()) {
177  vertex_num[v] = current_vertex++;
178  const auto& pt(save_complex.point(v));
179  for (auto x : pt)
180  stream << x << " ";
181  stream << std::endl;
182  }
183 
184  for (const auto & t : save_complex.triangle_range()) {
185  stream << "3 ";
186  for (auto x : t)
187  stream << vertex_num[x] << " ";
188  stream << std::endl;
189  }
190  stream.close();
191  } else {
192  std::cerr << "could not open file " << name_file << std::endl;
193  }
194  }
195 };
196 
197 } // namespace skeleton_blocker
198 
199 namespace skbl = skeleton_blocker;
200 
201 } // namespace Gudhi
202 
203 #endif // SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
bool read(OffVisitor &off_visitor)
Read an OFF file and calls the following methods :
Definition: Off_reader.h:65
Definition: SimplicialComplexForAlpha.h:26
Skeleton_blocker_off_reader(const std::string &name_file, Complex &read_complex, bool read_only_points=false, bool is_flag=false)
Definition: Skeleton_blocker_off_io.h:127
OFF file reader top class visitor.
Definition: Off_reader.h:43
Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
Definition: Skeleton_blocker_off_io.h:40
bool is_valid() const
Definition: Skeleton_blocker_off_io.h:146
Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
Definition: Skeleton_blocker_off_io.h:77
Class that allows to load a Skeleton_blocker_complex from an off file.
Definition: Skeleton_blocker_off_io.h:120
GUDHI  Version 2.2.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 15:00:55 for GUDHI by Doxygen 1.8.13