summaryrefslogtreecommitdiff
path: root/src/Skeleton_blocker/include/gudhi/Skeleton_blocker/Skeleton_blocker_off_io.h
blob: bc832d5d290ca6456061d8e724bf5ad8f478608c (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
 /*    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 Sophia Antipolis-Mediterranee (France)
  *
  *    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 SRC_SKELETON_BLOCKER_INCLUDE_GUDHI_SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_
#define SRC_SKELETON_BLOCKER_INCLUDE_GUDHI_SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_

#include <string>
#include <vector>

#include "gudhi/Off_reader.h"

namespace Gudhi {

namespace skbl {

/**
 *@brief Off reader visitor that can be passed to Off_reader to read a Skeleton_blocker_complex.
 */
template<typename Complex>
class Skeleton_blocker_off_visitor_reader {
  Complex& complex_;
  typedef typename Complex::Vertex_handle Vertex_handle;
  typedef typename Complex::Point Point;

  const bool load_only_points_;

 public:
  explicit Skeleton_blocker_off_visitor_reader(Complex& complex, bool load_only_points = false):
    complex_(complex),
    load_only_points_(load_only_points) {}


  void init(int dim, int num_vertices, int num_faces, int num_edges) {
    // todo do an assert to check that this number are correctly read
    // todo reserve size for vector points
  }


  void point(const std::vector<double>& point) {
    complex_.add_vertex(point);
  }

  void maximal_face(const std::vector<int>& face) {
    if (!load_only_points_) {
      for (size_t i = 0; i < face.size(); ++i)
        for (size_t j = i+1; j < face.size(); ++j) {
          complex_.add_edge(Vertex_handle(face[i]), Vertex_handle(face[j]));
        }
      }
  }

  void done() {}
};

/**
*@brief Class that allows to load a Skeleton_blocker_complex from an off file.
*/
template<typename Complex>
class Skeleton_blocker_off_reader {
 public:
  /**
   * name_file : file to read
   * read_complex : complex that will receive the file content
   * read_only_points : specify true if only the points must be read
   */
  Skeleton_blocker_off_reader(const std::string & name_file, Complex& read_complex, bool read_only_points = false):valid_(false) {
    std::ifstream stream(name_file);
    if (stream.is_open()) {
      Skeleton_blocker_off_visitor_reader<Complex> off_visitor(read_complex, read_only_points);
      Off_reader off_reader(stream);
      valid_ = off_reader.read(off_visitor);
    }
  }

  /**
   * return true iff reading did not meet problems.
   */
  bool is_valid() const {
    return valid_;
  }

 private:
  bool valid_;
};

}  // namespace skbl


}  // namespace Gudhi


#endif  // SRC_SKELETON_BLOCKER_INCLUDE_GUDHI_SKELETON_BLOCKER_SKELETON_BLOCKER_OFF_IO_H_