SPH
VectorRng.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "math/rng/Rng.h" // not really needed, but it doesn't make sense to use VectorRng without some Rng object
10 
12 
17 template <typename TScalarRng>
18 class VectorRng : public Noncopyable {
19 private:
20  TScalarRng rngImpl;
21 
22 public:
25  static_assert(!std::is_reference<TScalarRng>::value, "Cannot be used for references");
26  }
27 
28  template <typename... TArgs>
29  explicit VectorRng(TArgs&&... args)
30  : rngImpl(std::forward<TArgs>(args)...) {}
31 
33  : rngImpl(std::move(other.rngImpl)) {
34  static_assert(!std::is_reference<TScalarRng>::value, "Cannot be used for references");
35  }
36 
38  static_assert(!std::is_reference<TScalarRng>::value, "Cannot be used for references");
39  rngImpl = std::move(other.rngImpl);
40  return *this;
41  }
42 
44  return Vector(rngImpl(0), rngImpl(1), rngImpl(2));
45  }
46 
50  SPH_ASSERT(i >= 3);
51  return rngImpl(i);
52  }
53 };
54 
59 template <typename TScalarRng>
60 class VectorPdfRng : public Noncopyable {
61 private:
62  Box box;
63  VectorRng<TScalarRng> vectorRng;
64  Function<Float(const Vector&)> pdf;
65  Function<Float(const Vector&)> jacobian;
66  Float maxPdf;
67 
68 public:
77  const Box& box,
78  const Function<Float(const Vector&)>& pdf = [](const Vector&) { return 1._f; },
79  const Function<Float(const Vector&)>& jacobian = [](const Vector&) { return 1._f; },
80  const Float maximalPdf = 0._f)
81  : box(box)
82  , pdf(pdf)
83  , jacobian(jacobian)
84  , maxPdf(maximalPdf) {
85  if (maxPdf == 0._f) {
86  // step for finding pdf maximum
88  const Vector delta = 0.05_f * box.size();
90  box.iterate(delta, [&](const Vector& v) { //
91  this->maxPdf = max(this->maxPdf, this->pdf(v) * this->jacobian(v));
92  });
93  SPH_ASSERT(maxPdf > 0._f);
94  }
95  }
96 
98  SPH_ASSERT(getLength(box.size()) > 0._f);
99  for (Size i = 0; i < 10000; ++i) {
100  Vector v = vectorRng() * box.size() + box.lower();
101  const Float p = vectorRng.getAdditional(4) * maxPdf;
102  if (p < pdf(v) * jacobian(v)) {
103  return v;
104  }
105  }
106  SPH_ASSERT(false, "Couldn't generate vector in 1000 iterations");
107  return Vector(0._f);
108  }
109 };
110 
111 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Generic wrappers of lambdas, functors and other callables.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
#define NAMESPACE_SPH_END
Definition: Object.h:12
Random number generators.
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
BasicVector< Float > Vector
Definition: Vector.h:539
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE Vector size() const
Returns box dimensions.
Definition: Box.h:106
void iterate(const Vector &step, TFunctor &&functor) const
Execute functor for all possible values of vector (with constant stepping)
Definition: Box.h:168
Generic generator of random vectors using sampling with given PDF.
Definition: VectorRng.h:60
Vector operator()()
Definition: VectorRng.h:97
VectorPdfRng(const Box &box, const Function< Float(const Vector &)> &pdf=[](const Vector &) { return 1._f;}, const Function< Float(const Vector &)> &jacobian=[](const Vector &) { return 1._f;}, const Float maximalPdf=0._f)
Definition: VectorRng.h:76
Wrapper for generating random vectors.
Definition: VectorRng.h:18
VectorRng()
Default constructor. Can be only used if RNG is owned by the object.
Definition: VectorRng.h:24
VectorRng(TArgs &&... args)
Definition: VectorRng.h:29
Float getAdditional(const Size i)
Definition: VectorRng.h:49
Vector operator()()
Definition: VectorRng.h:43
VectorRng & operator=(VectorRng &&other)
Definition: VectorRng.h:37
VectorRng(VectorRng &&other)
Definition: VectorRng.h:32
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Object with deleted copy constructor and copy operator.
Definition: Object.h:54