summaryrefslogtreecommitdiff
path: root/src/common/include/gudhi/random_point_generators.h
blob: fb69f8327f2e9f773a7a24455c3d46d91d37fcaa (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author(s):       Clement Jamin
 *
 *    Copyright (C) 2016 Inria
 *
 *    Modification(s):
 *      - 2019/08 Vincent Rouvreau: Fix issue #10 for CGAL
 *      - YYYY/MM Author: Description of the modification
 */

#ifndef RANDOM_POINT_GENERATORS_H_
#define RANDOM_POINT_GENERATORS_H_

#include <CGAL/number_utils.h>
#include <CGAL/Random.h>
#include <CGAL/point_generators_d.h>
#include <CGAL/version.h>  // for CGAL_VERSION_NR

#include <vector>  // for vector<>

// Make compilation fail - required for external projects - https://github.com/GUDHI/gudhi-devel/issues/10
#if CGAL_VERSION_NR < 1041101000
# error Alpha_complex_3d is only available for CGAL >= 4.11
#endif

namespace Gudhi {

///////////////////////////////////////////////////////////////////////////////
// Note: All these functions have been tested with the CGAL::Epick_d kernel
///////////////////////////////////////////////////////////////////////////////

// construct_point: dim 2

template <typename Kernel>
typename Kernel::Point_d construct_point(const Kernel &k,
                                         typename Kernel::FT x1, typename Kernel::FT x2) {
  typename Kernel::FT tab[2];
  tab[0] = x1;
  tab[1] = x2;
  return k.construct_point_d_object()(2, &tab[0], &tab[2]);
}

// construct_point: dim 3

template <typename Kernel>
typename Kernel::Point_d construct_point(const Kernel &k,
                                         typename Kernel::FT x1, typename Kernel::FT x2, typename Kernel::FT x3) {
  typename Kernel::FT tab[3];
  tab[0] = x1;
  tab[1] = x2;
  tab[2] = x3;
  return k.construct_point_d_object()(3, &tab[0], &tab[3]);
}

// construct_point: dim 4

template <typename Kernel>
typename Kernel::Point_d construct_point(const Kernel &k,
                                         typename Kernel::FT x1, typename Kernel::FT x2, typename Kernel::FT x3,
                                         typename Kernel::FT x4) {
  typename Kernel::FT tab[4];
  tab[0] = x1;
  tab[1] = x2;
  tab[2] = x3;
  tab[3] = x4;
  return k.construct_point_d_object()(4, &tab[0], &tab[4]);
}

// construct_point: dim 5

template <typename Kernel>
typename Kernel::Point_d construct_point(const Kernel &k,
                                         typename Kernel::FT x1, typename Kernel::FT x2, typename Kernel::FT x3,
                                         typename Kernel::FT x4, typename Kernel::FT x5) {
  typename Kernel::FT tab[5];
  tab[0] = x1;
  tab[1] = x2;
  tab[2] = x3;
  tab[3] = x4;
  tab[4] = x5;
  return k.construct_point_d_object()(5, &tab[0], &tab[5]);
}

// construct_point: dim 6

template <typename Kernel>
typename Kernel::Point_d construct_point(const Kernel &k,
                                         typename Kernel::FT x1, typename Kernel::FT x2, typename Kernel::FT x3,
                                         typename Kernel::FT x4, typename Kernel::FT x5, typename Kernel::FT x6) {
  typename Kernel::FT tab[6];
  tab[0] = x1;
  tab[1] = x2;
  tab[2] = x3;
  tab[3] = x4;
  tab[4] = x5;
  tab[5] = x6;
  return k.construct_point_d_object()(6, &tab[0], &tab[6]);
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_plane(std::size_t num_points, int intrinsic_dim,
                                                               int ambient_dim,
                                                               double coord_min = -5., double coord_max = 5.) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;
  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    std::vector<FT> pt(ambient_dim, FT(0));
    for (int j = 0; j < intrinsic_dim; ++j)
      pt[j] = rng.get_double(coord_min, coord_max);

    Point p = k.construct_point_d_object()(ambient_dim, pt.begin(), pt.end());
    points.push_back(p);
    ++i;
  }
  return points;
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_moment_curve(std::size_t num_points, int dim,
                                                                      typename Kernel::FT min_x,
                                                                      typename Kernel::FT max_x) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;
  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    FT x = rng.get_double(min_x, max_x);
    std::vector<FT> coords;
    coords.reserve(dim);
    for (int p = 1; p <= dim; ++p)
      coords.push_back(std::pow(CGAL::to_double(x), p));
    Point p = k.construct_point_d_object()(
                                           dim, coords.begin(), coords.end());
    points.push_back(p);
    ++i;
  }
  return points;
}


// R = big radius, r = small radius
template <typename Kernel/*, typename TC_basis*/>
std::vector<typename Kernel::Point_d> generate_points_on_torus_3D(std::size_t num_points, double R, double r,
                                                                  bool uniform = false) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;

  // if uniform
  std::size_t num_lines = (std::size_t)sqrt(num_points);

  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    FT u, v;
    if (uniform) {
      std::size_t k1 = i / num_lines;
      std::size_t k2 = i % num_lines;
      u = 6.2832 * k1 / num_lines;
      v = 6.2832 * k2 / num_lines;
    } else {
      u = rng.get_double(0, 6.2832);
      v = rng.get_double(0, 6.2832);
    }
    Point p = construct_point(k,
                              (R + r * std::cos(u)) * std::cos(v),
                              (R + r * std::cos(u)) * std::sin(v),
                              r * std::sin(u));
    points.push_back(p);
    ++i;
  }
  return points;
}

// "Private" function used by generate_points_on_torus_d
template <typename Kernel, typename OutputIterator>
static void generate_uniform_points_on_torus_d(const Kernel &k, int dim, std::size_t num_slices,
                                               OutputIterator out,
                                               double radius_noise_percentage = 0.,
                                               std::vector<typename Kernel::FT> current_point =
                                                       std::vector<typename Kernel::FT>()) {
  CGAL::Random rng;
  int point_size = static_cast<int>(current_point.size());
  if (point_size == 2 * dim) {
    *out++ = k.construct_point_d_object()(point_size, current_point.begin(), current_point.end());
  } else {
    for (std::size_t slice_idx = 0; slice_idx < num_slices; ++slice_idx) {
      double radius_noise_ratio = 1.;
      if (radius_noise_percentage > 0.) {
        radius_noise_ratio = rng.get_double(
                                            (100. - radius_noise_percentage) / 100.,
                                            (100. + radius_noise_percentage) / 100.);
      }
      std::vector<typename Kernel::FT> cp2 = current_point;
      double alpha = 6.2832 * slice_idx / num_slices;
      cp2.push_back(radius_noise_ratio * std::cos(alpha));
      cp2.push_back(radius_noise_ratio * std::sin(alpha));
      generate_uniform_points_on_torus_d(
                                         k, dim, num_slices, out, radius_noise_percentage, cp2);
    }
  }
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_torus_d(std::size_t num_points, int dim, bool uniform = false,
                                                                 double radius_noise_percentage = 0.) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;

  std::vector<Point> points;
  points.reserve(num_points);
  if (uniform) {
    std::size_t num_slices = (std::size_t)std::pow(num_points, 1. / dim);
    generate_uniform_points_on_torus_d(
                                       k, dim, num_slices, std::back_inserter(points), radius_noise_percentage);
  } else {
    for (std::size_t i = 0; i < num_points;) {
      double radius_noise_ratio = 1.;
      if (radius_noise_percentage > 0.) {
        radius_noise_ratio = rng.get_double(
                                            (100. - radius_noise_percentage) / 100.,
                                            (100. + radius_noise_percentage) / 100.);
      }
      std::vector<typename Kernel::FT> pt;
      pt.reserve(dim * 2);
      for (int curdim = 0; curdim < dim; ++curdim) {
        FT alpha = rng.get_double(0, 6.2832);
        pt.push_back(radius_noise_ratio * std::cos(alpha));
        pt.push_back(radius_noise_ratio * std::sin(alpha));
      }

      Point p = k.construct_point_d_object()(pt.begin(), pt.end());
      points.push_back(p);
      ++i;
    }
  }
  return points;
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_sphere_d(std::size_t num_points, int dim, double radius,
                                                                  double radius_noise_percentage = 0.) {
  typedef typename Kernel::Point_d Point;
  Kernel k;
  CGAL::Random rng;
  CGAL::Random_points_on_sphere_d<Point> generator(dim, radius);
  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    Point p = *generator++;
    if (radius_noise_percentage > 0.) {
      double radius_noise_ratio = rng.get_double(
                                                 (100. - radius_noise_percentage) / 100.,
                                                 (100. + radius_noise_percentage) / 100.);

      typename Kernel::Point_to_vector_d k_pt_to_vec =
          k.point_to_vector_d_object();
      typename Kernel::Vector_to_point_d k_vec_to_pt =
          k.vector_to_point_d_object();
      typename Kernel::Scaled_vector_d k_scaled_vec =
          k.scaled_vector_d_object();
      p = k_vec_to_pt(k_scaled_vec(k_pt_to_vec(p), radius_noise_ratio));
    }
    points.push_back(p);
    ++i;
  }
  return points;
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_in_ball_d(std::size_t num_points, int dim, double radius) {
  typedef typename Kernel::Point_d Point;
  Kernel k;
  CGAL::Random rng;
  CGAL::Random_points_in_ball_d<Point> generator(dim, radius);
  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    Point p = *generator++;
    points.push_back(p);
    ++i;
  }
  return points;
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_in_cube_d(std::size_t num_points, int dim, double radius) {
  typedef typename Kernel::Point_d Point;
  Kernel k;
  CGAL::Random rng;
  CGAL::Random_points_in_cube_d<Point> generator(dim, radius);
  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    Point p = *generator++;
    points.push_back(p);
    ++i;
  }
  return points;
}

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_two_spheres_d(std::size_t num_points, int dim, double radius,
                                                                       double distance_between_centers,
                                                                       double radius_noise_percentage = 0.) {
  typedef typename Kernel::FT FT;
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::Vector_d Vector;
  Kernel k;
  CGAL::Random rng;
  CGAL::Random_points_on_sphere_d<Point> generator(dim, radius);
  std::vector<Point> points;
  points.reserve(num_points);

  std::vector<FT> t(dim, FT(0));
  t[0] = distance_between_centers;
  Vector c1_to_c2(t.begin(), t.end());

  for (std::size_t i = 0; i < num_points;) {
    Point p = *generator++;
    if (radius_noise_percentage > 0.) {
      double radius_noise_ratio = rng.get_double(
                                                 (100. - radius_noise_percentage) / 100.,
                                                 (100. + radius_noise_percentage) / 100.);

      typename Kernel::Point_to_vector_d k_pt_to_vec =
          k.point_to_vector_d_object();
      typename Kernel::Vector_to_point_d k_vec_to_pt =
          k.vector_to_point_d_object();
      typename Kernel::Scaled_vector_d k_scaled_vec =
          k.scaled_vector_d_object();
      p = k_vec_to_pt(k_scaled_vec(k_pt_to_vec(p), radius_noise_ratio));
    }

    typename Kernel::Translated_point_d k_transl =
        k.translated_point_d_object();
    Point p2 = k_transl(p, c1_to_c2);
    points.push_back(p);
    points.push_back(p2);
    i += 2;
  }
  return points;
}

// Product of a 3-sphere and a circle => d = 3 / D = 5

template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_3sphere_and_circle(std::size_t num_points,
                                                                            double sphere_radius) {
  typedef typename Kernel::FT FT;
  typedef typename Kernel::Point_d Point;
  Kernel k;
  CGAL::Random rng;
  CGAL::Random_points_on_sphere_d<Point> generator(3, sphere_radius);
  std::vector<Point> points;
  points.reserve(num_points);

  typename Kernel::Compute_coordinate_d k_coord =
      k.compute_coordinate_d_object();
  for (std::size_t i = 0; i < num_points;) {
    Point p_sphere = *generator++;  // First 3 coords

    FT alpha = rng.get_double(0, 6.2832);
    std::vector<FT> pt(5);
    pt[0] = k_coord(p_sphere, 0);
    pt[1] = k_coord(p_sphere, 1);
    pt[2] = k_coord(p_sphere, 2);
    pt[3] = std::cos(alpha);
    pt[4] = std::sin(alpha);
    Point p(pt.begin(), pt.end());
    points.push_back(p);
    ++i;
  }
  return points;
}

// a = big radius, b = small radius
template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_klein_bottle_3D(std::size_t num_points, double a, double b,
                                                                         bool uniform = false) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;

  // if uniform
  std::size_t num_lines = (std::size_t)sqrt(num_points);

  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    FT u, v;
    if (uniform) {
      std::size_t k1 = i / num_lines;
      std::size_t k2 = i % num_lines;
      u = 6.2832 * k1 / num_lines;
      v = 6.2832 * k2 / num_lines;
    } else {
      u = rng.get_double(0, 6.2832);
      v = rng.get_double(0, 6.2832);
    }
    double tmp = cos(u / 2) * sin(v) - sin(u / 2) * sin(2. * v);
    Point p = construct_point(k,
                              (a + b * tmp) * cos(u),
                              (a + b * tmp) * sin(u),
                              b * (sin(u / 2) * sin(v) + cos(u / 2) * sin(2. * v)));
    points.push_back(p);
    ++i;
  }
  return points;
}

// a = big radius, b = small radius
template <typename Kernel>
std::vector<typename Kernel::Point_d> generate_points_on_klein_bottle_4D(std::size_t num_points, double a, double b,
                                                                         double noise = 0., bool uniform = false) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;

  // if uniform
  std::size_t num_lines = (std::size_t)sqrt(num_points);

  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    FT u, v;
    if (uniform) {
      std::size_t k1 = i / num_lines;
      std::size_t k2 = i % num_lines;
      u = 6.2832 * k1 / num_lines;
      v = 6.2832 * k2 / num_lines;
    } else {
      u = rng.get_double(0, 6.2832);
      v = rng.get_double(0, 6.2832);
    }
    Point p = construct_point(k,
                              (a + b * cos(v)) * cos(u) + (noise == 0. ? 0. : rng.get_double(0, noise)),
                              (a + b * cos(v)) * sin(u) + (noise == 0. ? 0. : rng.get_double(0, noise)),
                              b * sin(v) * cos(u / 2) + (noise == 0. ? 0. : rng.get_double(0, noise)),
                              b * sin(v) * sin(u / 2) + (noise == 0. ? 0. : rng.get_double(0, noise)));
    points.push_back(p);
    ++i;
  }
  return points;
}


// a = big radius, b = small radius

template <typename Kernel>
std::vector<typename Kernel::Point_d>
generate_points_on_klein_bottle_variant_5D(
                                           std::size_t num_points, double a, double b, bool uniform = false) {
  typedef typename Kernel::Point_d Point;
  typedef typename Kernel::FT FT;
  Kernel k;
  CGAL::Random rng;

  // if uniform
  std::size_t num_lines = (std::size_t)sqrt(num_points);

  std::vector<Point> points;
  points.reserve(num_points);
  for (std::size_t i = 0; i < num_points;) {
    FT u, v;
    if (uniform) {
      std::size_t k1 = i / num_lines;
      std::size_t k2 = i % num_lines;
      u = 6.2832 * k1 / num_lines;
      v = 6.2832 * k2 / num_lines;
    } else {
      u = rng.get_double(0, 6.2832);
      v = rng.get_double(0, 6.2832);
    }
    FT x1 = (a + b * cos(v)) * cos(u);
    FT x2 = (a + b * cos(v)) * sin(u);
    FT x3 = b * sin(v) * cos(u / 2);
    FT x4 = b * sin(v) * sin(u / 2);
    FT x5 = x1 + x2 + x3 + x4;

    Point p = construct_point(k, x1, x2, x3, x4, x5);
    points.push_back(p);
    ++i;
  }
  return points;
}

}  // namespace Gudhi

#endif  // RANDOM_POINT_GENERATORS_H_