Gudhi  1.2.0
 All Classes Functions Variables Typedefs Friends Groups Pages
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 Sophia Antipolis-Mediterranee (France)
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 #ifndef SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
23 #define SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
24 
25 #include <gudhi/Off_reader.h>
26 
27 #include <string>
28 #include <vector>
29 #include <map>
30 
31 namespace Gudhi {
32 
33 namespace skbl {
34 
38 template<typename Complex>
40  Complex& complex_;
41  typedef typename Complex::Vertex_handle Vertex_handle;
42  typedef typename Complex::Point Point;
43 
44  const bool load_only_points_;
45 
46  public:
47  explicit Skeleton_blocker_off_flag_visitor_reader(Complex& complex, bool load_only_points = false) :
48  complex_(complex),
49  load_only_points_(load_only_points) { }
50 
51  void init(int dim, int num_vertices, int num_faces, int num_edges) {
52  // todo do an assert to check that this number are correctly read
53  // todo reserve size for vector points
54  }
55 
56  void point(const std::vector<double>& point) {
57  complex_.add_vertex(Point(point.begin(), point.end()));
58  }
59 
60  void maximal_face(const std::vector<int>& face) {
61  if (!load_only_points_) {
62  for (size_t i = 0; i < face.size(); ++i)
63  for (size_t j = i + 1; j < face.size(); ++j) {
64  complex_.add_edge(Vertex_handle(face[i]), Vertex_handle(face[j]));
65  }
66  }
67  }
68 
69  void done() { }
70 };
71 
75 template<typename Complex>
77  Complex& complex_;
78  typedef typename Complex::Vertex_handle Vertex_handle;
79  typedef typename Complex::Simplex_handle Simplex_handle;
80  typedef typename Complex::Point Point;
81 
82  const bool load_only_points_;
83  std::vector<Point> points_;
84  std::vector<Simplex_handle> maximal_faces_;
85 
86  public:
87  explicit Skeleton_blocker_off_visitor_reader(Complex& complex, bool load_only_points = false) :
88  complex_(complex),
89  load_only_points_(load_only_points) { }
90 
91  void init(int dim, int num_vertices, int num_faces, int num_edges) {
92  maximal_faces_.reserve(num_faces);
93  points_.reserve(num_vertices);
94  }
95 
96  void point(const std::vector<double>& point) {
97  points_.emplace_back(point.begin(), point.end());
98  }
99 
100  void maximal_face(const std::vector<int>& face) {
101  if (!load_only_points_) {
102  Simplex_handle s;
103  for (auto x : face)
104  s.add_vertex(Vertex_handle(x));
105  maximal_faces_.emplace_back(s);
106  }
107  }
108 
109  void done() {
110  complex_ = make_complex_from_top_faces<Complex>(maximal_faces_.begin(), maximal_faces_.end(),
111  points_.begin(), points_.end() );
112  }
113 };
114 
118 template<typename Complex>
120  public:
126  Skeleton_blocker_off_reader(const std::string & name_file, Complex& read_complex,
127  bool read_only_points = false, bool is_flag = false) : valid_(false) {
128  std::ifstream stream(name_file);
129  if (stream.is_open()) {
130  if (is_flag || read_only_points) {
131  Skeleton_blocker_off_flag_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
132  Off_reader off_reader(stream);
133  valid_ = off_reader.read(off_visitor);
134  } else {
135  Skeleton_blocker_off_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
136  Off_reader off_reader(stream);
137  valid_ = off_reader.read(off_visitor);
138  }
139  }
140  }
141 
145  bool is_valid() const {
146  return valid_;
147  }
148 
149  private:
150  bool valid_;
151 };
152 
153 template<typename Complex>
154 class Skeleton_blocker_off_writer {
155  public:
161  Skeleton_blocker_off_writer(const std::string & name_file, const Complex& save_complex) {
162  typedef typename Complex::Vertex_handle Vertex_handle;
163 
164  std::ofstream stream(name_file);
165  if (stream.is_open()) {
166  stream << "OFF\n";
167  size_t num_triangles = std::distance(save_complex.triangle_range().begin(), save_complex.triangle_range().end());
168  stream << save_complex.num_vertices() << " " << num_triangles << " 0 \n";
169 
170  // in case the complex has deactivated some vertices, eg only has vertices 0 2 5 7 for instance
171  // we compute a map from 0 2 5 7 to 0 1 2 3
172  std::map<Vertex_handle, size_t> vertex_num;
173  size_t current_vertex = 0;
174 
175  for (auto v : save_complex.vertex_range()) {
176  vertex_num[v] = current_vertex++;
177  const auto& pt(save_complex.point(v));
178  for (auto x : pt)
179  stream << x << " ";
180  stream << std::endl;
181  }
182 
183  for (const auto & t : save_complex.triangle_range()) {
184  stream << "3 ";
185  for (auto x : t)
186  stream << vertex_num[x] << " ";
187  stream << std::endl;
188  }
189  stream.close();
190  } else {
191  std::cerr << "could not open file " << name_file << std::endl;
192  }
193  }
194 };
195 
196 } // namespace skbl
197 
198 } // namespace Gudhi
199 
200 #endif // SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
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:126
Class that allows to load a Skeleton_blocker_complex from an off file.
Definition: Skeleton_blocker_off_io.h:119
bool read(OffVisitor &off_visitor)
Definition: Off_reader.h:70
Vertex_handle add_vertex()
Add a vertex to the complex with a default constructed associated point.
Definition: Skeleton_blocker_geometric_complex.h:96
Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
Definition: Skeleton_blocker_off_io.h:39
const Point & point(Vertex_handle v) const
Returns the Point associated to the vertex v.
Definition: Skeleton_blocker_geometric_complex.h:112
Definition: Off_reader.h:49
bool is_valid() const
Definition: Skeleton_blocker_off_io.h:145
Edge_handle add_edge(Vertex_handle a, Vertex_handle b)
Adds an edge between vertices a and b and all its cofaces.
Definition: Skeleton_blocker_complex.h:546
Complex_vertex_range vertex_range() const
Returns a Complex_vertex_range over all vertices of the complex.
Definition: Skeleton_blocker_complex.h:1245
Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
Definition: Skeleton_blocker_off_io.h:76
Complex_triangle_around_vertex_range triangle_range(Vertex_handle v) const
Range over triangles around a vertex of the simplicial complex. Methods .begin() and ...
Definition: Skeleton_blocker_complex.h:1321
Definition: SkeletonBlockerDS.h:60