random.cpp
Go to the documentation of this file.
1 //=============================================================================
2 // Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 // Author: Gonzalo Mier
4 // BSD-3 License
5 //=============================================================================
6 
8 
9 namespace f2c {
10 
11 Random::Random(uint32_t seed) : mt_(seed) {}
12 Random::~Random() = default;
13 Random::Random(const Random&) = default;
14 Random& Random::operator=(const Random&) = default;
15 Random::Random(Random&&) = default;
16 Random& Random::operator=(Random&&) = default;
17 
19  return getRandomLinear(0.0, 1.0);
20 }
21 
22 double Random::getRandomLinear(double min, double max) {
23  assert(min < max);
24  std::uniform_real_distribution<double> distribution(min, max);
25  return distribution(mt_);
26 }
27 
28 double Random::getRandomExp(double min, double max) {
29  assert(min < max);
30  double log_min {log(min)};
31  return exp((log(max) - log_min) * getRandomDouble() + log_min);
32 }
33 
34 double Random::getRandomExpDist(double lambda) {
35  std::exponential_distribution<double> distribution(lambda);
36  return distribution(mt_);
37 }
38 
40  return getRandomLinear(0.0, boost::math::constants::two_pi<double>());
41 }
42 
43 f2c::types::Cell Random::generateRandCell(double area, int n_sides,
44  double min_width, double max_width) {
45  if (n_sides <= 2) {
46  throw std::invalid_argument("Minimum 3 sides needed");
47  }
48  if (area <= 0) {
49  throw std::invalid_argument("The area should be positive");
50  }
52  for (size_t i = 0; i < n_sides; ++i) {
53  double r = this->getRandomLinear(min_width, max_width);
54  double alpha = 2.0 * M_PI * (i + this->getRandomDouble()) / n_sides;
55  border.addPoint(r * sin(alpha), r * cos(alpha));
56  }
57  border.addPoint(border.startPoint());
58  f2c::types::Cell cell {border};
59  cell *= sqrt(area / cell.area());
60  return cell;
61 }
62 
64  double min_width, double max_width) {
66  this->generateRandCell(area, n_sides, min_width, max_width)));
67 }
68 
69 f2c::types::Cell Random::genConvexCell(double area, size_t n_sides) {
71  do {
72  cell = this->generateRandCell(area, n_sides);
73  } while (!cell.isConvex());
74  return cell;
75 }
76 
77 f2c::types::Field Random::genConvexField(double area, size_t n_sides) {
79  this->genConvexCell(area, n_sides)));
80 }
81 
84  do {
85  cell = this->generateRandCell(area, 9);
86  } while (cell.isConvex());
87  return cell;
88 }
89 
92 }
93 
94 
95 } // namespace f2c
96 
97 
f2c::Random::genConvexField
f2c::types::Field genConvexField(double area, size_t n_sides=4)
Definition: random.cpp:77
f2c::Random::Random
Random(uint32_t seed=static_cast< uint32_t >(time(NULL)))
Definition: random.cpp:11
f2c::Random::generateRandField
f2c::types::Field generateRandField(double area, int n_sides, double min_width=0.5, double max_width=1.0)
Definition: random.cpp:63
f2c::types::Field
Definition: Field.h:18
1_basic_types.cell
cell
Definition: 1_basic_types.py:88
f2c::types::LinearRing::addPoint
void addPoint(double x, double y, double z=0)
Definition: LinearRing.cpp:105
f2c::Random::generateRandCell
f2c::types::Cell generateRandCell(double area, int n_sides, double min_width=0.5, double max_width=1.0)
Definition: random.cpp:43
f2c::Random::getRandomDouble
double getRandomDouble()
Definition: random.cpp:18
f2c::types::LinearRing
Definition: LinearRing.h:18
f2c::Random::getAngleRandom
double getAngleRandom()
Definition: random.cpp:39
f2c::types::LinearRing::startPoint
const Point startPoint() const
Definition: LinearRing.cpp:113
f2c::Random::genNonConvexCell
f2c::types::Cell genNonConvexCell(double area)
Definition: random.cpp:82
random.h
f2c::types::Cell
Definition: Cell.h:32
f2c::types::Cells
Definition: Cells.h:21
f2c::Random::getRandomLinear
double getRandomLinear(double min, double max)
Definition: random.cpp:22
f2c::Random
Definition: random.h:23
f2c
Main namespace of the fields2cover library.
Definition: boustrophedon_decomp.h:14
f2c::Random::genNonConvexField
f2c::types::Field genNonConvexField(double area)
Definition: random.cpp:90
f2c::Random::getRandomExpDist
double getRandomExpDist(double lambda)
Definition: random.cpp:34
f2c::Random::genConvexCell
f2c::types::Cell genConvexCell(double area, size_t n_sides=4)
Definition: random.cpp:69
f2c::Random::getRandomExp
double getRandomExp(double min, double max)
Definition: random.cpp:28
f2c::Random::mt_
std::mt19937 mt_
Definition: random.h:88


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31