generators.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): Siargey Kachanovich
4  *
5  * Copyright (C) 2015 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef GENERATORS_H_
12 #define GENERATORS_H_
13 
14 #include <CGAL/Epick_d.h>
15 #include <CGAL/point_generators_d.h>
16 #include <CGAL/Random.h>
17 
18 #include <fstream>
19 #include <string>
20 #include <vector>
21 #include <cmath>
22 
23 using K = CGAL::Epick_d<CGAL::Dynamic_dimension_tag>;
24 using FT = K::FT;
25 using Point_d = K::Point_d;
26 using Point_Vector = std::vector<Point_d>;
27 using Random_cube_iterator = CGAL::Random_points_in_cube_d<Point_d>;
28 using Random_point_iterator = CGAL::Random_points_in_ball_d<Point_d>;
29 
34 inline void
35 off_reader_cust(std::string file_name, std::vector<Point_d> & points) {
36  std::ifstream in_file(file_name.c_str(), std::ios::in);
37  if (!in_file.is_open()) {
38  std::cerr << "Unable to open file " << file_name << std::endl;
39  return;
40  }
41  std::string line;
42  double x;
43  // Line OFF. No need in it
44  if (!getline(in_file, line)) {
45  std::cerr << "No line OFF\n";
46  return;
47  }
48  // Line with 3 numbers. No need
49  if (!getline(in_file, line)) {
50  std::cerr << "No line with 3 numbers\n";
51  return;
52  }
53  // Reading points
54  while (getline(in_file, line)) {
55  std::vector< double > point;
56  std::istringstream iss(line);
57  while (iss >> x) {
58  point.push_back(x);
59  }
60  points.push_back(Point_d(point));
61  }
62  in_file.close();
63 }
64 
70 inline void
71 read_points_cust(std::string file_name, Point_Vector & points) {
72  std::ifstream in_file(file_name.c_str(), std::ios::in);
73  if (!in_file.is_open()) {
74  std::cerr << "Unable to open file " << file_name << std::endl;
75  return;
76  }
77  std::string line;
78  double x;
79  while (getline(in_file, line)) {
80  std::vector< double > point;
81  std::istringstream iss(line);
82  while (iss >> x) {
83  point.push_back(x);
84  }
85  Point_d p(point.begin(), point.end());
86  if (point.size() != 1)
87  points.push_back(p);
88  }
89  in_file.close();
90 }
91 
99 void generate_points_grid(Point_Vector& W, int width, int D, bool torus) {
100  int nb_points = 1;
101  for (int i = 0; i < D; ++i)
102  nb_points *= width;
103  for (int i = 0; i < nb_points; ++i) {
104  std::vector<double> point;
105  int cell_i = i;
106  for (int l = 0; l < D; ++l) {
107  if (torus)
108  point.push_back(-1 + (2.0 / (width - 1))*(cell_i % width));
109  else
110  point.push_back(-1 + (2.0 / width)*(cell_i % width));
111  // attention: the bottom and the right are covered too!
112  cell_i /= width;
113  }
114  W.push_back(point);
115  }
116 }
117 
121 void generate_points_random_box(Point_Vector& W, int nbP, int dim) {
122  Random_cube_iterator rp(dim, 1.0);
123  for (int i = 0; i < nbP; i++) {
124  W.push_back(*rp++);
125  }
126 }
127 
131 void generate_points_sphere(Point_Vector& W, int nbP, int dim) {
132  CGAL::Random_points_on_sphere_d<Point_d> rp(dim, 1);
133  for (int i = 0; i < nbP; i++)
134  W.push_back(*rp++);
135 }
136 
140 void generate_points_torus(Point_Vector& W, int nbP, int dim) {
141  CGAL::Random rand;
142  const double pi = std::acos(-1);
143  for (int i = 0; i < nbP; i++) {
144  std::vector<FT> point;
145  for (int j = 0; j < dim; j++) {
146  double alpha = rand.uniform_real(static_cast<double>(0), 2*pi);
147  point.push_back(sin(alpha));
148  point.push_back(cos(alpha));
149  }
150  W.push_back(Point_d(point));
151  }
152 }
153 
154 #endif // GENERATORS_H_
GUDHIdev  Version 3.5.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 16 2022 14:01:50 for GUDHIdev by Doxygen 1.9.1