Skeleton_blocker_off_io.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): David Salinas
4  *
5  * Copyright (C) 2014 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
12 #define SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
13 
14 #include <gudhi/Off_reader.h>
15 
16 #include <string>
17 #include <vector>
18 #include <map>
19 
20 namespace Gudhi {
21 
22 namespace skeleton_blocker {
23 
27 template<typename Complex>
29  Complex& complex_;
30  typedef typename Complex::Vertex_handle Vertex_handle;
31  typedef typename Complex::Point Point;
32 
33  const bool load_only_points_;
34 
35  public:
36  explicit Skeleton_blocker_off_flag_visitor_reader(Complex& complex, bool load_only_points = false) :
37  complex_(complex),
38  load_only_points_(load_only_points) { }
39 
40  void init(int dim, int num_vertices, int num_faces, int num_edges) {
41  // TODO(DS): do an assert to check that this number are correctly read
42  // TODO(DS): reserve size for vector points
43  }
44 
45  void point(const std::vector<double>& point) {
46  complex_.add_vertex(Point(point.begin(), point.end()));
47  }
48 
49  void maximal_face(const std::vector<int>& face) {
50  if (!load_only_points_) {
51  for (size_t i = 0; i < face.size(); ++i)
52  for (size_t j = i + 1; j < face.size(); ++j) {
53  complex_.add_edge_without_blockers(Vertex_handle(face[i]), Vertex_handle(face[j]));
54  }
55  }
56  }
57 
58  void done() { }
59 };
60 
64 template<typename Complex>
66  Complex& complex_;
67  typedef typename Complex::Vertex_handle Vertex_handle;
68  typedef typename Complex::Simplex Simplex;
69  typedef typename Complex::Point Point;
70 
71  const bool load_only_points_;
72  std::vector<Point> points_;
73  std::vector<Simplex> maximal_faces_;
74 
75  public:
76  explicit Skeleton_blocker_off_visitor_reader(Complex& complex, bool load_only_points = false) :
77  complex_(complex),
78  load_only_points_(load_only_points) { }
79 
80  void init(int dim, int num_vertices, int num_faces, int num_edges) {
81  maximal_faces_.reserve(num_faces);
82  points_.reserve(num_vertices);
83  }
84 
85  void point(const std::vector<double>& point) {
86  points_.emplace_back(point.begin(), point.end());
87  }
88 
89  void maximal_face(const std::vector<int>& face) {
90  if (!load_only_points_) {
91  Simplex s;
92  for (auto x : face)
93  s.add_vertex(Vertex_handle(x));
94  maximal_faces_.emplace_back(s);
95  }
96  }
97 
98  void done() {
99  complex_ = make_complex_from_top_faces<Complex>(maximal_faces_.begin(), maximal_faces_.end(),
100  points_.begin(), points_.end());
101  }
102 };
103 
107 template<typename Complex>
109  public:
115  Skeleton_blocker_off_reader(const std::string & name_file, Complex& read_complex,
116  bool read_only_points = false, bool is_flag = false) : valid_(false) {
117  std::ifstream stream(name_file);
118  if (stream.is_open()) {
119  if (is_flag || read_only_points) {
120  Skeleton_blocker_off_flag_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
121  Off_reader off_reader(stream);
122  valid_ = off_reader.read(off_visitor);
123  } else {
124  Skeleton_blocker_off_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
125  Off_reader off_reader(stream);
126  valid_ = off_reader.read(off_visitor);
127  }
128  }
129  }
130 
134  bool is_valid() const {
135  return valid_;
136  }
137 
138  private:
139  bool valid_;
140 };
141 
142 template<typename Complex>
143 class Skeleton_blocker_off_writer {
144  public:
150  Skeleton_blocker_off_writer(const std::string & name_file, const Complex& save_complex) {
151  typedef typename Complex::Vertex_handle Vertex_handle;
152 
153  std::ofstream stream(name_file);
154  if (stream.is_open()) {
155  stream << "OFF\n";
156  size_t num_triangles = std::distance(save_complex.triangle_range().begin(), save_complex.triangle_range().end());
157  stream << save_complex.num_vertices() << " " << num_triangles << " 0 \n";
158 
159  // in case the complex has deactivated some vertices, eg only has vertices 0 2 5 7 for instance
160  // we compute a map from 0 2 5 7 to 0 1 2 3
161  std::map<Vertex_handle, size_t> vertex_num;
162  size_t current_vertex = 0;
163 
164  for (auto v : save_complex.vertex_range()) {
165  vertex_num[v] = current_vertex++;
166  const auto& pt(save_complex.point(v));
167  for (auto x : pt)
168  stream << x << " ";
169  stream << std::endl;
170  }
171 
172  for (const auto & t : save_complex.triangle_range()) {
173  stream << "3 ";
174  for (auto x : t)
175  stream << vertex_num[x] << " ";
176  stream << std::endl;
177  }
178  stream.close();
179  } else {
180  std::cerr << "could not open file " << name_file << std::endl;
181  }
182  }
183 };
184 
185 } // namespace skeleton_blocker
186 
187 namespace skbl = skeleton_blocker;
188 
189 } // namespace Gudhi
190 
191 #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:51
Definition: SimplicialComplexForAlpha.h:14
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:115
OFF file reader top class visitor.
Definition: Off_reader.h:29
Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
Definition: Skeleton_blocker_off_io.h:28
bool is_valid() const
Definition: Skeleton_blocker_off_io.h:134
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15
Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
Definition: Skeleton_blocker_off_io.h:65
Class that allows to load a Skeleton_blocker_complex from an off file.
Definition: Skeleton_blocker_off_io.h:108
GUDHI  Version 3.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 11 2020 11:09:13 for GUDHI by Doxygen 1.8.13